Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct student {
- char name[10];
- int score;
- };
- void swap(struct student *studen1, struct student *student2){
- struct student temp = *studen1;
- *studen1 = *student2;
- *student2 = temp;
- }
- void quicksort(struct student *students, int low, int high)
- {
- int pivotnumber;
- int i, j;//looping
- if (low<high)
- {
- pivotnumber = low;
- i = low;
- j = high;
- while (i<j)
- {
- while ((students[i].score <= students[pivotnumber].score) && (i<high))i++;
- while (students[j].score>students[pivotnumber].score)j--;
- if (i<j)
- swap(&students[i], &students[j]);
- }
- swap(&students[pivotnumber], &students[j]);
- quicksort(students, low, j - 1);
- quicksort(students, j + 1, high);
- }
- }
- void * scoresDescendingSort(struct student *students, int len) {
- if (students == NULL || len <= 0)
- return NULL;
- quicksort(students, 0, len);
- return students;
- }
- int main(){
- int i;
- struct student students[]={{"adsfasdf",10},{"adsfasdf",1},{"asdfasdf",56}};
- scoresDescendingSort(students,3);
- for(i=0;i<3;i++){
- printf("%d,",students[i].score);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement