Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sort.h"
- #include <stdio.h>
- void sortAlphabetically(artist artistArray[MAX_ARTISTS]){
- quickSort(artistArray[0].artistsSongs, 0, MAX_SONGS - 1);
- }
- void quickSort(char songs[MAX_SONGS][MAX_LENGTH], int left, int right) {
- printf("left = %d and right = %d\n",left, right);
- if (left >= right) {
- return;
- }
- char pivot[MAX_LENGTH];
- strcpy(pivot, songs[MAX_SONGS/2]);
- printf("the pivot is %s\n", pivot);
- int index = partition(songs, left, right, pivot);
- printf("the index is %d\n", index);
- quickSort(songs, left, index - 1);
- quickSort(songs, index, right);
- }
- int partition(char songs[MAX_SONGS][MAX_LENGTH], int left, int right, char pivot[MAX_LENGTH]){
- printf("in partition left is %d and right is %d\n", left, right);
- char temp[MAX_LENGTH];
- while (left <= right) {
- while ( strcmp(songs[left], pivot) < 0 ) {
- left++;
- printf("left is at %d\n", left);
- }
- while ( strcmp(pivot, songs[right] ) < 0) {
- right--;
- printf("right is at%d\n", right);
- }
- if (left <= right) {
- strcpy(temp, songs[left]);
- printf("%s changed to %s", songs[left], songs[right] );
- strcpy(songs[left], songs[right]);
- printf("%s changed to %s", songs[right], temp);
- strcpy(songs[right], temp);
- left++;
- right--;
- }
- }
- printf("%d is being returned\n", left);
- return left;
- }
Advertisement