Understanding sorting algorithms

For the bubble sort algorithm listed below, I don't understand at which step does a key comparison occurs and at which step an item assignment occurs. I do understand how the algorithm works though. In other word I don't understand what you call as key comparison and what you call as item assignment.
Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void bubbleSort(int list[], int length)
{
    int index;
    int temp;
    int iteration;

    for (iteration=1; iteration<length; iteration++)
    {
        for (index=0; index<length-iteration; index++)
            if (list[index] > list[index+1])
            {
                temp = list[index];
                list[index] = list[index+1];
                list[index+1] = temp;
            }
    }
}
1
2
3
4
5
6
            if (list[index] > list[index+1]) //comparison
            {
                temp = list[index]; //assignment
                list[index] = list[index+1]; //assignment
                list[index+1] = temp; //assignment
            }
Thank you. Really appreciate your help.
How about the same question for an insertion sort

void insertionSort(int list3[], int listLength)
{
int temp;
int loc;
int firstOutOfOrder;

for (firstOutOfOrder=1; firstOutOfOrder<listLength; firstOutOfOrder++)
{
if (list3[firstOutOfOrder] < list3[firstOutOfOrder-1])
{
loc = firstOutOfOrder;
temp = list3[firstOutOfOrder];

do
{
list3[loc] = list3[loc-1];
loc--;
}
while(loc>0 && list3[loc-1]>temp);

list3[loc] = temp;
}
}
}
Last edited on
Topic archived. No new replies allowed.