diff --git a/gmon.out b/gmon.out index 98de536..e4ccea3 100755 Binary files a/gmon.out and b/gmon.out differ diff --git a/quicksort.c b/quicksort.c new file mode 100644 index 0000000..cc5abea --- /dev/null +++ b/quicksort.c @@ -0,0 +1,67 @@ +/*algorithm quicksort(A, lo, hi) is + // Ensure indices are in correct order + if lo >= hi || lo < 0 then + return + + // Partition array and get the pivot index + p := partition(A, lo, hi) + + // Sort the two partitions + quicksort(A, lo, p - 1) // Left side of pivot + quicksort(A, p + 1, hi) // Right side of pivot + +// Divides array into two partitions +algorithm partition(A, lo, hi) is + pivot := A[hi] // Choose the last element as the pivot + + // Temporary pivot index + i := lo + + for j := lo to hi - 1 do + // If the current element is less than or equal to the pivot + if A[j] <= pivot then + // Swap the current element with the element at the temporary pivot index + swap A[i] with A[j] + // Move the temporary pivot index forward + i := i + 1 + + // Swap the pivot with the last element + swap A[i] with A[hi] + return i // the pivot index*/ + +int Partition(int * array, int lo, int hi){ + /*variables*/ + int pivot = array[hi]; + int tmp_ind = lo; + int tmp; + int i = 0; + + /*pivots*/ + for(i=lo; i=hi)||(lo<0)){ + return; + } + + p = Partition(array, lo, hi); + + quicksort(array, lo, p-1); + quicksort(array, p+1, hi); +} \ No newline at end of file diff --git a/quicksort.h b/quicksort.h new file mode 100644 index 0000000..72d914f --- /dev/null +++ b/quicksort.h @@ -0,0 +1,7 @@ +#ifndef __QUICKSORT__ +#define __QUICKSORT__ + +int Partition(int * array, int lo, int hi); +void quicksort(int * array, int lo, int hi); + +#endif \ No newline at end of file diff --git a/student_rank b/student_rank index 177bf60..651a0e3 100755 Binary files a/student_rank and b/student_rank differ diff --git a/student_rank.c b/student_rank.c index dca9ed6..2c13be4 100644 --- a/student_rank.c +++ b/student_rank.c @@ -5,6 +5,7 @@ #include #include "heapsort.h" #include "bubblesort.h" +#include "quicksort.h" void generate_grades(int** students_array, int students_number, int grades_number) { @@ -77,7 +78,7 @@ void sort_students(int** students_rank, int** students_array, int students_numbe { grades[j] = students_array[j][i]; } - heapsort(grades,students_number); + quicksort(grades,0,students_number); for(j = 0; j < students_number; j++) { students_rank[j][i] = find_rank_student(students_array[j][i],grades,students_number);