Hogy-Bear

sort.c

Feb 9th, 2018
263
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include "sort.h"
  2. #include <stdio.h>
  3.  
  4. void sortAlphabetically(artist artistArray[MAX_ARTISTS]){
  5.   quickSort(artistArray[0].artistsSongs, 0, MAX_SONGS - 1);
  6. }
  7.  
  8. void quickSort(char songs[MAX_SONGS][MAX_LENGTH], int left, int right) {
  9.   printf("left = %d and right = %d\n",left, right);
  10.   if (left >= right) {
  11.     return;
  12.   }
  13.   char pivot[MAX_LENGTH];
  14.   strcpy(pivot, songs[MAX_SONGS/2]);
  15.   printf("the pivot is %s\n", pivot);
  16.   int index = partition(songs, left, right, pivot);
  17.   printf("the index is %d\n", index);
  18.   quickSort(songs, left, index - 1);
  19.   quickSort(songs, index, right);
  20. }
  21.  
  22. int partition(char songs[MAX_SONGS][MAX_LENGTH], int left, int right, char pivot[MAX_LENGTH]){
  23.   printf("in partition left is %d and right is %d\n", left, right);
  24.   char temp[MAX_LENGTH];
  25.   while (left <= right) {
  26.  
  27.     while ( strcmp(songs[left], pivot) < 0 ) {
  28.       left++;
  29.       printf("left is at %d\n", left);
  30.     }
  31.  
  32.     while ( strcmp(pivot, songs[right] ) < 0) {
  33.       right--;
  34.       printf("right is at%d\n", right);
  35.     }
  36.  
  37.     if (left <= right) {
  38.       strcpy(temp, songs[left]);
  39.       printf("%s changed to %s", songs[left], songs[right] );
  40.       strcpy(songs[left], songs[right]);
  41.       printf("%s changed to %s", songs[right], temp);
  42.       strcpy(songs[right], temp);
  43.       left++;
  44.       right--;
  45.     }
  46.   }
  47.   printf("%d is being returned\n", left);
  48.   return left;
  49. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment