# Why doesn't my sort algorithm work

``````//Example program #1 from Chapter 25 of Absolute Beginner's Guide
//to C, 3rd Edition
//File Chapter25ex1.c

/*This program declares and initializes an arrays of character
pointers and then asks for ratings associated */

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int i;
char ans;

//Declaring our array of 9 characters and the initializing them
char* movies[9] = {"Amour", "Argo", "Beasts of the Southern Wild", "Django Unchained", "Les Misarables", "Life of Pi", "Lincoln", "Silver Linings Playbook", "Zero Dark Thirty"};
int movieratings[9]; // A corresponding array of 9 integers for movie ratings

char* tempmovie = "This will be used to sort rated movies";
int outer, inner, didSwap, temprating; //for the sort loop

printf("\n\n*** Oscar Season 2012 is here! ***\n\n");
printf("Time to rate this year's best picture nominees:");

for (i = 0; i < 9; i++)
{
printf("\nDid you see %s? (Y/N):", movies[i]);
scanf(" %c", &ans);
if ((toupper(ans)) == 'Y')
{
printf("\nWhat was your rating on a scale ");
printf("of 1-10 ");
scanf(" %d", &movieratings[i]);
continue;
}
else
{
movieratings[i] = -1;
}
}

//Now sort the movies by rating (the unseen will go to the bottom)

for(outer = 0; outer < 8; outer++)
{
didSwap = 0;
for(inner = outer; inner < 9; inner++)
{
if (movieratings[inner] < movieratings[outer])
{
tempmovie = movies[inner];
temprating = movieratings[inner];
movies[inner] = movies[outer];
movieratings[inner] = movieratings[outer];
movies[outer] = tempmovie;
movieratings[outer] = temprating;
didSwap = 1;
}
else if (didSwap == 0)
{
break;
}
}
}
//Now to print movies you saw in order
printf("\n\n** Your Movie Ratings for the 2012 Oscar Contenders **\n");
for (i = 0; i < 9; i++)
{
if (movieratings[i] != -1)
{
printf("%s rated at %d!\n", movies[i], movieratings[i]);
}
}
}
``````

Your problem is in your sorting. Check the Wikipedia article about bubble sort implementation: https://en.wikipedia.org/wiki/Bubble_sort#Implementation. In the most non-optimized case, the pseudo-code looks like this

```procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
```

What I want you to notice, is that in the `for-loop`, the comparison is between `A[i]` and `A[i-1]`.

``````for(outer = 0; outer < 8; outer++)
{
didSwap = 0;
for(inner = outer; inner < 9; inner++)
{
if (movieratings[inner] < movieratings[outer])
{
tempmovie = movies[inner];
temprating = movieratings[inner];
movies[inner] = movies[outer];
movieratings[inner] = movieratings[outer];
movies[outer] = tempmovie;
movieratings[outer] = temprating;
didSwap = 1;
}
else if (didSwap == 0)
{
break;
}
}
}
``````

you are comparing `movieratings[inner]` with `movieratings[outer]`. Which is wrong. What you are supposed to do, is compare each pair of values, at places `i` and `i+1`, and if `movierating[i]` is bigger than `movieratings[i+1]` then change them.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!