For the homework I had to create a quick sort function, and then use a quicksort function in a linked list. Here's a screenshot of both parts of the homework: Just the quick list part #include <stdio.h> #include <stdlib.h> //void quicksort(int[], int, int); //int partition( int[], int, int); int partition(int a[], int p, int r) { int i,j, pivot, temp; pivot = a[p]; i = p; j = r; while(1) { while(a[i] < pivot && a[i] != pivot) i++; while(a[j] > pivot && a[j] != pivot) j--; if (i <j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } else { return j; } } } void quicksort(int a[], int p, int r) { if (p < r) { int q; q = partition(a, p ,r); quicksort(a,p,q); quicksort(a, q+1, r); } } int main(void) { struct sort; int a[]= {25, 52, 37, 63, 14, 17, 8, 6}; int i; quicksort( a, 0, 7); printf("Sorted array is: "); for(i=0; i<8; ++i) printf(" %d", a[i]); return 0; } ...
Comments
Post a Comment