Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<stdio.h>
  2. struct student {
  3. char name[10];
  4. int score;
  5. };
  6. void swap(struct student *studen1, struct student *student2){
  7. struct student temp = *studen1;
  8. *studen1 = *student2;
  9. *student2 = temp;
  10. }
  11. void quicksort(struct student *students, int low, int high)
  12. {
  13. int pivotnumber;
  14. int i, j;//looping
  15. if (low<high)
  16. {
  17. pivotnumber = low;
  18. i = low;
  19. j = high;
  20.  
  21. while (i<j)
  22. {
  23. while ((students[i].score <= students[pivotnumber].score) && (i<high))i++;
  24. while (students[j].score>students[pivotnumber].score)j--;
  25. if (i<j)
  26. swap(&students[i], &students[j]);
  27. }
  28. swap(&students[pivotnumber], &students[j]);
  29. quicksort(students, low, j - 1);
  30. quicksort(students, j + 1, high);
  31. }
  32. }
  33. void * scoresDescendingSort(struct student *students, int len) {
  34. if (students == NULL || len <= 0)
  35. return NULL;
  36. quicksort(students, 0, len);
  37. return students;
  38. }
  39. int main(){
  40. int i;
  41. struct student students[]={{"adsfasdf",10},{"adsfasdf",1},{"asdfasdf",56}};
  42. scoresDescendingSort(students,3);
  43. for(i=0;i<3;i++){
  44. printf("%d,",students[i].score);
  45. }
  46. return 0;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement