Can anyone spot a problem with my quick sort implementation below? It seems to fail on arrays with more than 10 or so elements.
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void generateRandom(int arr[], int size)
{
srand(time(NULL));
int i;
for (i = 0; i < size; i++)
{
arr[i] = rand() % 100;
}
}
int partition(int arr[], int start, int end)
{
int i = start, j = end;
int pivot = arr[start];
for (;;)
{
for (; arr[i] < pivot; i++);
for (; arr[j] > pivot; j--);
if (i < j)
{
swap(&arr[i], &arr[j]);
}
else
{
return j;
}
}
}
void quickSort(int arr[], int start, int end)
{
int part;
if (start < end)
{
part = partition(arr, start, end);
quickSort(arr, start, part);
quickSort(arr, part + 1, end);
}
}
int main()
{
generateRandom(arr, 100);
for (i = 0; i < 100; i++)
{
printf("%d ", arr[i]);
}
printf("\n\n");
quickSort(arr, 0, 99);
for (i = 0; i < 100; i++)
{
printf("%d ", arr[i]);
}
printf("\n\n");
return 0;
}
First, you code doesn't compile. When I made the corrections to make it compile (adding stdio.h, and definitions for arr and i in main) it infinite looped, which it will do if the partition starts and ends with the pivot. You need to increment and decrement before the comparisons rather than after. You can do that by starting with i = start-1 and j = end+1 and changing your inner loops to increment or decrement first, or you can leave them as is and just do an i++ and j-- after the swap -- I did that and the sort works.
Note that your pivot choice is poor for already sorted arrays; you really should be picking the median of 3 or even 9 values.
P.S. Other desirable optimizations: 1) Switch to an insertion sort for small partitions -- the optimal cutoff point is machine-dependent. Another approach is to not sort partitions below a certain size, and then do an insertion sort on the whole array after quicksort is done. It's also possible to use heapsort instead of insertion sort if done carefully; google introsort. 2) quicksort does two recursive calls; eliminate the second one by setting start = part + 1 and looping. 3) Avoid the possibility of stack overflow by quicksorting the larger partition first. 4) Eliminate the first recursive call by using an explicit stack. 5) Inline swap() and partition(). 6) Don't bother with any of that and just call the qsort library routine. :-)
I had the same problem,
but I changed my while loop to do..while and it worked.
This is my new code now.
int partition(int a[], int lo, int hi) {
int v = a[lo], i = lo, j = hi;
do {
do {
i++;
} while(a[i] < v) ;
do {
j--;
}while(a[j] > v) ;
if(i < j) interchange(&a[i], &a[j]);
}while(i < j);
interchange(&a[lo], &a[j]);
return j;
}
Related
Has this function been written correctly?
It seems that something is wrong when I try to run the function with a large number of elements in the array (eg 1000).
Then its appears to stop.
int quick_sort(int n, int tablica[],int b, int a)
{
if(a==n-1 || n==0) return;
if(b==n-1)
{
b=0;
a++;
}
if(tablica[b]>tablica[b+1])
{
bufor=tablica[b];
tablica[b]=tablica[b+1];
tablica[b+1]=bufor;
}
b++;
return quick_sort(n,tablica,b,a);
}
Above code will not work even for a small array, unless the small array is unsorted in a particular way. It compares one element with the next element. If the array is say {4,3,8,7,1} the sort will fail, because it has no mechanism to push 1 to the start of the array.
For larger arrays there are too many recursion and the program hits the stack limit and simply fails.
You can use recursion in quicksort but the number of recursions has to be kept in check. For example for array of size 1000 you don't want to have to more than 1000 recursion. Example:
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int arr[], int low, int high)
{
if(low < high)
{
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high - 1; j++)
{
if(arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
int pi = i + 1;
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
int main()
{
int arr[] = { 7,3,6,1,4,8,9,2 };
int arrsize = sizeof(arr) / sizeof(*arr);
quicksort(arr, 0, arrsize - 1);
for(int i = 0; i < arrsize; i++)
printf("%d\n", arr[i]);
return 0;
}
I am creating a program which uses a pointer to function to allow the user to select from 2 options to perform on an array of integers: reverse or randomise. However, the program yield no output when executed. Where did I go wrong?
void reverseArray(int arraySize, int a[]);
void randomiseArray(int arraySize, int a[]);
void printArray(int arraySize, int a[], void (*arrayFunction)(int arraySize, int a[]));
int main ()
{
srand(time(NULL));
int selection;
int myArray[] = {2,4,6,8,10,12,14,16,18,20};
printf("Enter 0 for reverse, 1 for randomise.\n");
scanf("%d",&selection);
if (selection == 0)
printArray(sizeof(myArray)/sizeof(myArray[0]),myArray,reverseArray);
else if (selection == 1)
printArray(sizeof(myArray)/sizeof(myArray[0]),myArray,randomiseArray);
else
printf("Please make a valid selection\n");
return 0;
}
void reverseArray(int arraySize, int a[])
{
int i,j=arraySize,swap=0;
for (i=0;i<arraySize/2;i++)
{
swap = a[i];
a[i] = a[j];
a[j] = swap;
--j;
}
}
void randomiseArray(int arraySize, int a[])
{
int i,j,swap;
for (i=0;i<arraySize;i++)
{
j = rand()%(i+1);
if (j!=i)
{
swap = a[i];
a[i] = a[j];
a[i] = swap;
}
}
}
void printArray(int arraySize, int a[], void (*arrayFunction)(int arraySize, int a[]))
{
arrayFunction(arraySize, a);
for (int i = 0; i < arraySize; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
The user selects when function they want to perform by typing either 0 for reverse or 1 for randomise. Based on the decision, a printArray function is called with the respective choice.
reverseArray is just a generic swap function, and randomiseArray uses a Fisher-Yates shuffling algorithm.
I am assuming that the source of my error lies in the printArray fucntion, yet I cannot correct it.
Two errors
in reverseArray, j should start at arraysize - 1 or you will be writing one element past the end of the array
void reverseArray(int arraySize, int a[])
{
int i, j = arraySize - 1, swap = 0;
for (i = 0; i<arraySize / 2; i++)
{
swap = a[i];
a[i] = a[j];
a[j] = swap;
--j;
}
}
And in randomizearray(), you assign to a[i] twice, instead of using a[j] for the final assignment.
void randomiseArray(int arraySize, int a[])
{
int i, j, swap;
for (i = 0; i<arraySize; i++)
{
j = rand() % (i + 1);
printf("%d %d\n", i, j);
if (j != i)
{
swap = a[i];
a[i] = a[j];
a[j] = swap;
}
}
}
The first error causes undefined behavior when reversing, the second causes the array to stay the same. Other than that, the function pointers are setup fine and work correctly.
In reverseArray() your index j is out of range, it should he j=arraySize-1
Other than that it works for me.
You are accessing outside the bounds of your array, causing UB and in this case "no output":
In reverseArray, you must initialize j=arraySize-1. Without the -1 you go out of bounds.
I am a complete beginner to stackoverflow and this is my first post. Please forgive if this is not the correct place to post these kinds of queries. I have written code for the Quicksort algorithm, based on the algorithm given in the Algorithms course in Coursera(It is not for any assignments though).
Basically, there are two functions Quicksort which is called recursively and partition() function that returns the index of the pivot. I select the pivot as the first element of the array every time. I checked the partition() function and it works fine but the array is not sorted even after I call the Quicksort() function.
Any help is appreciated. Thanks.
#include <stdio.h>
void swap(int *p, int i, int j)
{
int temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
}
int partition(int *q, int l, int r)
{
int i = l+1, j;
int p = l;
int len = r-l +1;
for (j = l+1; j < len; j++)
{
/*printf("%d \n", j);*/
if ( *(q+j) < *(q+p) )
{
swap(q, i, j);
i += 1;
}
}
swap(q, l, i-1);
/*printf("%d", i-1);*/
return (i-1);
}
void quicksort(int *ptr, int low, int high)
{
if (low < high)
{
int p = partition(ptr, low, high);
printf("%d\n", p);
quicksort(ptr, low, p);
quicksort(ptr, p+1, high);
}
}
int main(){
int i;
int a[] = {3, 8, 2, 5, 1, 4, 7, 6};
int len = sizeof(a)/sizeof(a[0]);
for ( i = 0; i < len; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
int *ptr = a;
quicksort(ptr, 0, len-1);
for (i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
2 corrections.
Small one: Change 3rd line inside if block in QuickSort function
from
quicksort(ptr, low, p);
to
quicksort(ptr, low, p-1);
This will improve performance.
Main error:
Your partition function is wrong. Specifically the loop where j runs from l+1 to r-l+1, because, r-l+1 can be less than l+1
I'll write the partition function for you if you want (post a comment if you face any problem with that) though I'd advice you to do it yourself.
EDIT:
A possible partition function:
int partition(int *q, int l, int r){
int i,j;
int p = *(q + l);
for(i = l + 1, j = r; ;){
while(*(q + i) <= p)
i++;
while(*(q + j) >= p)
j--;
if(i >= j)
break;
swap(q, i, j);
}
return i;
}
Changes noted in comments.
int partition(int *q, int l, int r)
{
int i = l+1, j;
int p = l;
/* fix: int len = r-l+1; is not used */
for (j = l+1; j <= r; j++) /* fix: j <= r */
{
if ( *(q+j) <= *(q+p) ) /* fix: <= */
{
swap(q, i, j);
i += 1;
}
}
swap(q, l, i-1);
return (i-1);
}
void quicksort(int *ptr, int low, int high)
{
if (low < high)
{
int p = partition(ptr, low, high);
quicksort(ptr, low, p-1); /* optimization: p-1 */
quicksort(ptr, p+1, high);
}
}
If interested, Hoare partition scheme is faster. If you switch to this, don't forget to change the two quicksort calls to quicksort(lo, p) and quicksort(p+1, hi) ). You might want to change the Hoare pivot to pivot = A[(lo+hi)/2], which will avoid worst case issue with sorted or reverse sorted array.
http://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme
I am doing a merge sort program in C but I'm getting some unexpected output.
Can anyone find out error in the program?
#include<stdio.h>
int array[100],n;
void merge(int l,int m,int h);
void mergesort(int start,int end);
main(){
int i;
printf("Enter Size: ");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&array[i]);
}
mergesort(0, n-1);
for(i=0;i<n;i++){
printf("%d\n",array[i]);
}
}
void mergesort(int start,int end){
int mid;
if(start<end){
mid=(start+end)/2;
mergesort(start,mid);
mergesort(mid+1,end);
merge(start,mid,end);
}
}
void merge(int start,int mid,int end){
int i,j,k;
int a1[100],a2[100];
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a1[mid]=999;
a2[j]=999;
i=0;j=0;
for(k=start; k <=end; k++)
{
if(a1[i]<=a2[j])
array[k]=a1[i++];
else
array[k]=a2[j++];
}
}
Output:
Enter Size: 5
1 2 3 2 3
2
2
3
3
-1818025592
For larger arrays, the output is more scrambled. I think there is an error in merge() function.
You are terminating your a1 and a2 arrays at the wrong location.
Try changing:
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a1[mid]=999;
a2[j]=999;
to
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
a1[k]=999;
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a2[k]=999;
When you create your temporary arrays:
for (k = 0, i = start; i <= mid; i++, k++) {
a1[k] = array[i];
}
a1[i] = 999;
you put a sentinel value with a high value at the end. But i is the index into the original array. You must use k, the index of the temporary array a1:
for (k = 0, i = start; i <= mid; i++, k++) {
a1[k] = array[i];
}
a1[k] = 999;
Even so, the double control structure with k and i is clumsy. And there's no need to guess a high number; <limits.h> has constants defined for the min and max values of most types:
k = 0;
for (i = start; i <= mid; i++) {
a1[k++] = array[i];
}
a1[k] = INT_MAX;
Here, you increment k when an item is appended. This works even when the assignment happens conditionally, i.e. not in all iterations.
Finally, I would recommend to use exclusive upper bounds. That's the natural way to express ranges in C. mid would then be both the exclusive upper bound of the left array and the inclusive lower bound of the right array.
I have solved it
a[mid + 1]=999
a[k]=999
I'm trying to use this to sort randomly generated numbers, so I print the random numbers, apply the quicksort and then print the sorted list, but it just gives me the numbers in the same order again.
void quicksort(double x[], int first, int last)
{
int i=first;
int j=last;
if((j-i)<2)
{return;}
else
{
double pivot;
pivot=x[(i+j)/2];
if(i<j)
{
if(x[i]<pivot)
{ ++i; printf("%f\n", i); }
else if(x[j]>pivot)
{ --j; }
else if(x[i]>=pivot && x[j]<=pivot)
{
swap( x[i], x[j] );
++i;
--j;
}
}
quicksort(x, first, i);
quicksort(x, i+1, last);
}
}
You're not showing swap().
If that's a function, there's no way it can succeed since it only gets the values of x[i] and x[i], not any addresses.
If it's a macro, it might work, but of course it's hard to tell without seeing it.
There are a few issues with your implementation. The one that's causing your current problem is most likely the swap function, which if you tested it, I'm guessing probably wouldn't do any swapping. I suggest you replace it with something like this:
void
swap(double a[], int i, in j)
{
double t = a[i];
a[i] = a[j]; a[j] = t;
}
But then after that, there will be other issues in your implementation, because you are not partitioning the list properly.
Here's a reference implementation (I'm taking the pivot to be the first element in the range, which is non-optimal, but not less optimal than picking the middle element).
void
qsort(double a[], int lo, int hi)
{
int i = lo, j = hi - 1;
int pivot = a[i++];
if(i+1 > j) return;
while(i<j)
{
while(i < hi && a[i] < pivot) i++;
while(j > lo && a[j] > pivot) j--;
if(i<j) swap(a, i, j);
}
swap(a, lo, j);
qsort(a, lo, j);
qsort(a, j+1, hi);
}