Hogy-Bear icon

sort.c

Hogy-Bear | PRO | 02/09/18 11:53:00 PM UTC | 0 ⭐ | 221 👁️ | Never ⏰ | []
C |

1.38 KB

|

None

|

0 👍

/

0 👎

#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;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎