You are on page 1of 52

CMPT 125

A P P L I C AT I O N S O F A R R AY S
S O RT I N G

© Janice Regan, CMPT 125, 2023 1


Putting numbers into order
There are many approaches to taking an array of numbers and putting
those numbers into ascending or descending order.
◦ Insertion Sort
◦ Selection Sort
◦ Quick Sort
◦ Bubble Sort

© Janice Regan, CMPT 125, 2023 2


Selection Sort
A method to transform the unsorted array into a sorted array.
This example sorts into ascending order, sorting into descending
order is a parallel process.
Apply a simple algorithm to an array of length M
1. Start with N=M
2. Consider the last N elements in the array, A[M-N] to A[M-1]
3. Find the element that contains the smallest value in the group of elements
defined in (2). Record the index k at which this smallest value is found.
Smallest = A[k]
4. Swap the smallest value with the (M-N)th element
◦ A[M-N] swapped with A[k] the smallest element
5. Decrease N by 1 and return to step 2 (until N<=0)

© Janice Regan, CMPT 125, 2023 3


Ascending / Descending Order

Unsorted Array A[13]


23 3 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Sorted Array A[13]: Descending order


95 86 75 46 37 32 23 19 16 12 7 5 3
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Sorted Array A[13]: Ascending order


3 5 7 12 16 19 23 32 37 46 75 86 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 4


Sample Selection Sort: 1
Unsorted Array A[13]
23 3 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 1: N=M
23 3 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[1], switch with A[M-N]=A[0]

After Iteration 1: N=M


3 23 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 5


Sample Selection Sort: 2
3 23 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 2: N=M-1
3 23 12 19 7 75 46 95 37 86 5 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[10], switch with A[M-N]=A[1]

After Iteration 2: N=M-1


3 5 12 19 7 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 6


Sample Selection Sort: 3
3 5 12 19 7 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 3: N=M-2
3 5 12 19 7 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[4], switch with A[M-N]=A[2]

After Iteration 3: N=M-2


3 5 7 19 12 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 7


Sample Selection Sort: 4
3 5 7 19 12 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 4: N=M-3
3 5 7 19 12 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[4], switch with A[M-N]=A[3]

After Iteration 4: N=M-3


3 5 7 12 19 75 46 95 37 86 23 16 32

A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 8


Sample Selection Sort: 5
3 5 7 12 19 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 5: N=M-4
3 5 7 12 19 75 46 95 37 86 23 16 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[11], switch with A[M-N]=A[4]

After Iteration 5: N=M-4


3 5 7 12 16 75 46 95 37 86 23 19 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 9


Sample Selection Sort: 6
3 5 7 12 16 75 46 95 37 86 23 19 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 6: N=M-5
3 5 7 12 16 75 46 95 37 86 23 19 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[11], switch with A[M-N]=A[5]

After Iteration 6: N=M-5


3 5 7 12 16 19 46 95 37 86 23 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 10


Sample Selection Sort: 7
3 5 7 12 16 19 46 95 37 86 23 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 7: N=M-6
3 5 7 12 16 19 46 95 37 86 23 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[10], switch with A[M-N]=A[6]

After Iteration 7: N=M-6


3 5 7 12 16 19 23 95 37 86 46 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 11


Sample Selection Sort: 8
3 5 7 12 16 19 23 95 37 86 46 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 8: N=M-7
3 5 7 12 16 19 23 95 37 86 46 75 32
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[12], switch with A[M-N]=A[7]

After Iteration 8: N=M-7


3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 12


Sample Selection Sort: 9
3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 9: N=M-8
3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[8], switch with A[M-N]=A[8]

After Iteration 9: N=M-8


3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 13


Sample Selection Sort: 10
3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 10: N=M-9


3 5 7 12 16 19 23 32 37 86 46 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[10], switch with A[M-N]=A[9]

After Iteration 10: N=M-9


3 5 7 12 16 19 23 32 37 46 86 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 14


Sample Selection Sort: 11
3 5 7 12 16 19 23 32 37 46 86 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 11: N=M-10


3 5 7 12 16 19 23 32 37 46 86 75 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[11], switch with A[M-N]=A[10]

After Iteration 11: N=M-10


3 5 7 12 16 19 23 32 37 46 75 86 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 15


Sample Selection Sort: 11
3 5 7 12 16 19 23 32 37 46 75 86 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Iteration 12: N=M-10


3 5 7 12 16 19 23 32 37 46 75 86 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

Smallest element is A[12], switch with A[M-N]=A[12]

After Iteration 13: N=M-10


3 5 7 12 16 19 23 32 37 46 75 86 95
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

© Janice Regan, CMPT 125, 2023 16


Quicksort
void quickSort( int set[], int start, int end)
{
int pivotPoint = 1;

if( start < end)


{
/* Get the pivot point. */
pivotPoint = partition(set, start, end);
/* Sort the left hand half */
quickSort(set, start, pivotPoint-1);
/* Sort the second half. */
quickSort(set, pivotPoint+1, end);
}
}

© Janice Regan, CMPT 129 2017 17


QuickSort Partitioning:
one method
int partition ( int set[], int start, int end)
{
int pivotValue = 0; int pivotIndex= 0; int mid = 0;
mid= (start+end)/2;
swap(set[start], set[mid];
pivotValue = set[start] /* red arrow */
for ( int scan – start+1; scan <=end; scan++)
{
/* scan is black arrow */
if(set[scan] < pivotValue)
{
pivotIndex++;
swap(set[pivotIndex], set[scan]);
}
}
swap( set[start], set[pivotIndex]);
return pivotIndex;
} © Janice Regan, CMPT 129 2017 18
Sample Quick Sort: 1
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
23 3 12 19 7 75 48 95 37 86 5 16 32
Pivot, middle element
3 Less than pivot but pivot = index so just increment pivotindex to 2
48 3 12 19 7 75 23 95 37 86 5 16 32

12 Less than pivot, but pivot = index so increment pivotindex to 3


48 3 12 19 7 75 23 95 37 86 5 16 32
Pivot Two steps skipped before next illustrated step, till pivotindex=5
More than pivot, no action, move to next element, index of next element is 5
48 3 12 19 7 75 23 95 37 86 5 16 32
Pivot
Location 6 23 < pivot, swap with location pivotindex = 5, then increment pivotindex to 6
48 3 12 19 7 23 75 95 37 86 5 16 32
Pivot
© Janice Regan, CMPT 129 2017
19
Sample Quick Sort: 2
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

48 3 12 19 7 23 75 95 37 86 5 16 32

More than pivot, no action, move to next element, index of next element is 8, pivotIndex still 6
48 3 12 19 7 23 75 95 37 86 5 16 32

37 Less than pivot, swap with location pivotindex = 6, then increment pivotindex to 7
48 3 12 19 7 23 37 95 75 86 5 16 32
Pivot
More than pivot, no action, move to next element, index of next element is 9, pivotIndex still 7
48 3 12 19 7 23 37 95 75 86 5 16 32
Pivot
5 is Less than pivot, swap with location pivotindex = 7, then increment pivotindex to 8
48 3 12 19 7 23 37 5 75 86 95 16 32
Pivot

© Janice Regan, CMPT 129 2017


Sample Quick Sort: 3
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
16 is Less than pivot, swap with location pivotindex =8, then increment pivotindex to 9
48 3 12 19 7 23 37 5 16 86 95 75 32
Pivot
32 is Less than pivot, swap with location pivotindex =9, then increment pivotindex to 10
48 3 12 19 7 23 37 5 16 32 95 75 86
Pivot
Less than pivot More than pivot
32 3 12 19 7 23 37 5 16 48 95 75 86
Pivot

32 3 12 19 7 23 37 5 16 48 95 75 86

Less than pivot


32 3 12 19 7 23 37 5 16 48 95 75 86
Pivot, middle element

© Janice Regan, CMPT 129 2017 21


Sample Quick Sort: 4
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot increment pivotindex to 2
7 3 12 19 32 23 37 5 16 48 95 75 86
Pivot
More than pivot no change to pivotindex still 2
7 3 12 19 32 23 37 5 16 48 95 75 86
Pivot
More than pivot no change to pivotindex still 2
7 3 12 19 32 23 37 5 16 48 95 75 86
Pivot
Less than pivot exchange and increment pivotindex to 3
7 3 5 19 32 23 37 12 16 48 95 75 86

7 3 5 19 32 23 37 12 16 48 95 75 86

© Janice Regan, CMPT 129 2017 22


Sample Quick Sort: 5
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot More than pivot
5 3 7 19 32 23 37 12 16 48 95 75 86
Pivot

5 3 7 19 32 23 37 12 16 48 95 75 86

3 5 7 19 32 23 37 12 16 48 95 75 86

3 5 7 19 32 23 37 12 16 48 95 75 86
Pivot, middle element
Pivot
3 5 7 23 32 19 37 12 16 48 95 75 86

23
© Janice Regan, CMPT 129 2017
Sample Quick Sort: 6
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 5 7 23 32 19 37 12 16 48 95 75 86
Pivot

3 5 7 23 19 32 37 12 16 48 95 75 86
Pivot

3 5 7 23 19 12 37 32 16 48 95 75 86
Pivot

3 5 7 23 19 12 16 32 37 48 95 75 86
Pivot

3 5 7 16 19 12 23 32 37 48 95 75 86

© Janice Regan, CMPT 129 2017


Pivot

24
Sample Quick Sort: 7
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 5 7 16 19 12 23 32 37 48 95 75 86

3 5 7 16 19 12 23 32 37 48 95 75 86
Pivot

3 5 7 19 16 12 23 32 37 48 95 75 86
Pivot

3 5 7 12 16 19 23 32 37 48 95 75 86
Pivot

3 5 7 12 16 19 23 23 37 48 95 75 86

© Janice Regan, CMPT 129 2017 25


Sample Quick Sort: 8
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Pivot
3 5 7 12 16 19 23 32 37 48 95 75 86
First element larger than pivot

3 5 7 12 16 19 23 32 37 48 75 95 86
Pivot

3 5 7 12 16 19 23 32 37 48 75 95 86

3 5 7 12 16 19 23 32 37 48 75 86 95
Pivot

© Janice Regan, CMPT 129 2017 26


QuickSort Partitioning:
Alternate partitioning Method
int partition ( int set[], int start, int end)
{
int pivotValue = 0; int mid = 0;
mid= (start+end)/2; pivotValue = set[mid];
swap(set[start], set[mid]);
loSwap = start; hiSwap=end;
do
{ while (loSwap <= hiSwap && InArray[loSwap] <= pivot)
{ loSwap++; }
while (InArray[hiSwap] > pivot)
{ hiSwap--; }
if (loSwap < hiSwap)
{ swap(InArray[loSwap], InArray[hiSwap] ); }
} while (loSwap < hiSwap);
swap(set[start], set[hiSwap]);
return hiSwap;
} © Janice Regan, CMPT 129 2017 27
Sample Quick Sort: 1
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
23 3 12 19 7 75 48 95 37 86 5 16 32
Pivot, middle element

Less than pivot


48 3 12 19 7 75 23 95 37 86 5 16 32
Pivot First element larger than pivot
Less than pivot
48 3 12 19 7 75 23 95 37 86 5 16 32
Pivot First element smaller than pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 95 37 86 5 16 75
Pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 95 37 86 5 16 75
Pivot First element larger than pivot
© Janice Regan, CMPT 129 2017
28
Sample Quick Sort: 2
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot More than pivot
48 3 12 19 7 32 23 95 37 86 5 16 75
Pivot First element smaller than pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 16 37 86 5 95 75
Pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 16 37 86 5 95 75
Pivot First element larger than pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 16 37 86 5 95 75
Pivot
First element smaller than pivot
Less than pivot
48 3 12 19 7 32 23 16 37 5 86 95 75
Pivot More than pivot
© Janice Regan, CMPT 129 2017 29
Sample Quick Sort: 3
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot More than pivot
48 3 12 19 7 32 23 16 37 5 86 95 75
Pivot First element larger than pivot
Less than pivot More than pivot
48 3 12 19 7 32 23 16 37 5 86 95 75
Pivot First element smaller than pivot
Less than pivot More than pivot
5 3 12 19 7 32 23 16 37 48 86 95 75
Pivot

5 3 12 19 7 32 23 16 37 48 86 95 75

Less than pivot


5 3 12 19 7 32 23 16 37 48 86 95 75
Pivot, middle element

© Janice Regan, CMPT 129 2017 30


Sample Quick Sort: 4
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot
7 3 12 19 5 32 23 16 37 48 86 95 75
Pivot
First element larger than pivot
Less than pivot
7 3 12 19 5 32 23 16 37 48 86 95 75
Pivot
First element smaller than pivot
Less than pivot More than pivot
7 3 5 19 12 32 23 16 37 48 86 95 75
Pivot
Less than pivot
7 3 5 19 12 32 23 16 37 48 86 95 75
First element larger than pivot
Less than pivot
7 3 5 19 12 32 23 16 37 48 86 95 75

© Janice Regan, CMPT 129 2017 31


Sample Quick Sort: 5
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Less than pivot More than pivot
5 3 7 19 12 32 23 16 37 48 86 95 75
Pivot

5 3 7 19 12 32 23 16 37 48 86 95 75

3 5 7 19 12 32 23 16 37 48 86 95 75

3 5 7 19 37 16 32 23 12 48 86 95 75
Pivot, middle element
Pivot
3 5 7 16 37 19 32 23 12 48 86 95 75

© Janice Regan, CMPT 129 2017


First element larger than pivot 32
Sample Quick Sort: 6
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 5 7 16 37 19 32 23 12 48 86 95 75
Pivot First element smaller than pivot

3 5 7 16 12 19 32 23 37 48 86 95 75
Pivot

3 5 7 16 12 19 32 23 37 48 86 95 75
Pivot
First element larger than pivot
Pivot More than pivot
3 5 7 16 12 19 32 23 37 48 86 95 75
First element smaller than pivot
More than pivot
3 5 7 12 16 19 32 23 37 48 86 95 75

© Janice Regan, CMPT 129 2017


Pivot
Sample Quick Sort: 7
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 5 7 12 16 19 32 23 37 48 86 95 75

3 5 7 12 16 19 32 23 37 48 86 95 75
Pivot
Pivot
3 5 7 12 16 32 19 23 37 48 86 95 75

Pivot
3 5 7 12 16 32 19 23 37 48 86 95 75
First element larger than pivot

3 5 7 12 16 32 19 23 37 48 86 95 75
First element smaller than pivot Pivot
First element smaller than pivot
© Janice Regan, CMPT 129 2017 34
Sample Quick Sort: 8
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 5 7 12 16 23 19 32 37 48 86 95 75
Pivot

3 5 7 12 16 23 19 32 37 48 86 95 75

3 5 7 12 16 19 23 32 37 48 86 95 75

3 5 7 12 16 19 23 32 37 48 86 95 75
Pivot
Pivot
3 5 7 12 16 19 23 32 37 48 95 86 75

© Janice Regan, CMPT 129 2017 35


Sample Quick Sort: 9

A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
Pivot
3 5 7 12 16 19 23 32 37 48 95 86 75
First element larger than pivot

3 5 7 12 16 19 23 32 37 48 75 86 95
Pivot

3 5 7 12 16 19 23 32 37 48 75 86 95

3 5 7 12 16 19 23 32 37 48 75 86 95
Pivot

© Janice Regan, CMPT 129 2017 36


Selection sort function
void SelectionSort(int inArray[], int N, int M, int L)
{
int index = 0;
int smallestIndex = 0;
int minIndex = 0;
int temp = 0;

if( limitTest(N, M, L) == FALSE )


{
printf("Inconsistent input, sort omitted \n“);
return;
}
if (N >= M)
{ return; }

© Janice Regan, CMPT 125, 2023 37


Selection sort function
for(index = N; index < M; index++)
{
smallestIndex = index;
for(minIndex=index+1; minIndex < M+1; minIndex++)
{
if(inArray[minIndex] < inArray[smallestIndex])
{
smallestIndex = minIndex;
}
}
temp = inArray[smallestIndex];
inArray[smallestIndex] = inArray[index];
inArray[index] = temp;
}
}

© Janice Regan, CMPT 125, 2023 38


Bubble Sort
A method to transform the unsorted array into a sorted array.
This example sorts into ascending order, sorting into descending
order is a parallel process.
Apply a simple algorithm to an array of length M
1. Start with N=0
2. Consider the array elements A[0] to A[M-1-N]
3. For each I, 0 <= i <M-1-N
a) Compare A[i] and A[i+1] and put in order
4. A[M-1-N] now contains the largest number, it is in order
5. Increase N by 1 and return to step 2 (until N=M-1)

© Janice Regan, CMPT 125, 2023 39


Sample Bubble Sort: 1
Iteration 1: N=M
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
23 3 12 19 7 75 46 95 37 86 5 16 32
Compare A[M-N] and A[M-N+I], put in order
3 23 12 19 7 75 46 95 37 86 5 16 32
Compare A[M-N+1] and A[M-N+2], put in order
3 12 23 19 7 75 46 95 37 86 5 16 32
Compare A[M-N+2] and A[M-N+3], put in order
3 12 19 23 7 75 46 95 37 86 5 16 32
Compare A[M-N+3] and A[M-N+4], put in order
3 12 19 7 23 75 46 95 37 86 5 16 32
Compare A[M-N+4] and A[M-N+5], put in order
3 12 19 7 23 75 46 95 37 86 5 16 32
Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 125, 2023 40


Sample Bubble Sort: 2
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 12 19 7 23 46 75 95 37 86 5 16 32
Compare A[M-N+6] and A[M-N+7], put in order
3 12 19 7 23 46 75 95 37 86 5 16 32
Compare A[M-N+7] and A[M-N+8], put in order
3 12 19 7 23 46 75 37 95 86 5 16 32
Compare A[M-N+8] and A[M-N+9], put in order
3 12 19 7 23 46 75 37 86 95 5 16 32
Compare A[M-N+9] and A[M-N+10], put in order
3 12 19 7 23 46 75 37 86 5 95 16 32
Compare A[M-N+10] and A[M-N+11], put in order
3 12 19 7 23 46 75 37 86 5 16 95 32
Compare A[M-N+11] and A[M-N+12], put in order
3 12 19 7 23 46 75 37 86 5 16 32 95

© Janice Regan, CMPT 125, 2023 41


Sample Bubble Sort: 3
Iteration 2: N=M-1
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 12 19 7 23 46 75 37 86 5 16 32 95
Compare A[M-N] and A[M-N+I], put in order
3 12 19 7 23 46 75 37 86 5 16 32 95
Compare A[M-N+1] and A[M-N+2], put in order
3 12 19 7 23 46 75 37 86 5 16 32 95
Compare A[M-N+2] and A[M-N+3], put in order
3 12 7 19 23 46 75 37 86 5 16 32 95
Compare A[M-N+3] and A[M-N+4], put in order
3 12 7 19 23 46 75 37 86 5 16 32 95
Compare A[M-N+4] and A[M-N+5], put in order
3 12 7 19 23 46 75 37 86 5 16 32 95
Compare A[M-N+5] and A[M-N+6], put in order

© Janice Regan, CMPT 125, 2023 42


Sample Bubble Sort: 4
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 12 7 19 23 46 75 37 86 5 16 32 95

Compare A[M-N+6] and A[M-N+7], put in order


3 12 7 19 23 46 37 75 86 5 16 32 95
Compare A[M-N+7] and A[M-N+8], put in order
3 12 7 19 23 46 37 75 86 5 16 32 95
Compare A[M-N+8] and A[M-N+9], put in order
3 12 7 19 23 46 37 75 5 86 16 32 95
Compare A[M-N+9] and A[M-N+10], put in order
3 12 7 19 23 46 37 75 5 16 86 32 95
Compare A[M-N+9] and A[M-N+10], put in order
3 12 7 19 23 46 37 75 5 16 32 86 95

© Janice Regan, CMPT 125, 2023 43


Sample Bubble Sort: 5
Iteration 3: N=M-2
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 12 7 19 23 46 37 75 5 16 32 86 95
Compare A[M-N] and A[M-N+I], put in order
3 12 7 19 23 46 37 75 5 16 32 86 95
Compare A[M-N+1] and A[M-N+2], put in order
3 7 12 19 23 46 37 75 5 16 32 86 95
Compare A[M-N+2] and A[M-N+3], put in order
3 7 12 19 23 46 37 75 5 16 32 86 95
Compare A[M-N+3] and A[M-N+4], put in order
3 7 12 19 23 46 37 75 5 16 32 86 95
Compare A[M-N+4] and A[M-N+5], put in order
3 7 12 19 23 46 37 75 5 16 32 86 95
Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 125, 2023 44
Sample Bubble Sort: 6
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 7 12 19 23 37 46 75 5 16 32 86 95

Compare A[M-N+6] and A[M-N+7], put in order


3 7 12 19 23 37 46 75 5 16 32 86 95

Compare A[M-N+7] and A[M-N+8], put in order


3 7 12 19 23 37 46 5 75 16 32 86 95
Compare A[M-N+8] and A[M-N+9], put in order
3 7 12 19 23 37 46 5 16 75 32 86 95
Compare A[M-N+9] and A[M-N+10], put in order
3 7 12 19 23 37 46 5 16 32 75 86 95

© Janice Regan, CMPT 125, 2023 45


Sample Bubble Sort: 7
Iteration 4: N=M-3
No changes in first 6 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 7 12 19 23 37 46 5 16 32 75 86 95

Compare A[M-N+6] and A[M-N+7], put in order


3 7 12 19 23 37 5 46 16 32 75 86 95

Compare A[M-N+7] and A[M-N+8], put in order


3 7 12 19 23 37 5 16 46 32 75 86 95
Compare A[M-N+8] and A[M-N+9], put in order
3 7 12 19 23 37 5 16 32 46 75 86 95

© Janice Regan, CMPT 125, 2023 46


Sample Bubble Sort: 8
Iteration 5: N=M-4
No changes in first 5 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 7 12 19 23 37 5 16 32 46 75 86 95

Compare A[M-N+5] and A[M-N+6], put in order


3 7 12 19 23 5 37 16 32 46 75 86 95

Compare A[M-N+6] and A[M-N+7], put in order


3 7 12 19 23 5 16 37 32 46 75 86 95

Compare A[M-N+7] and A[M-N+8], put in order


3 7 12 19 23 5 16 32 37 46 75 86 95

© Janice Regan, CMPT 125, 2023 47


Sample Bubble Sort: 9
Iteration 6: N=M-5
No changes in first 4 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 7 12 19 23 5 16 32 37 46 75 86 95

Compare A[M-N+4] and A[M-N+5], put in order


3 7 12 19 5 23 16 32 37 46 75 86 95

Compare A[M-N+5] and A[M-N+6], put in order


3 7 12 19 5 16 23 32 37 46 75 86 95

Compare A[M-N+6] and A[M-N+7], put in order


3 7 12 19 5 16 23 32 37 46 75 86 95

© Janice Regan, CMPT 125, 2023 48


Sample Bubble Sort: 10
Iteration 7: N=M-6
No changes in first 3 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]

3 7 12 19 5 16 23 32 37 46 75 86 95

Compare A[M-N+3] and A[M-N+4], put in order


3 7 12 5 19 16 23 32 37 46 75 86 95

Compare A[M-N+4] and A[M-N+5], put in order


3 7 12 5 16 19 23 32 37 46 75 86 95

Compare A[M-N+5] and A[M-N+6], put in order


3 7 12 5 16 19 23 32 37 46 75 86 95

© Janice Regan, CMPT 125, 2023 49


Sample Bubble Sort: 11
Iteration 8: N=M-7
No changes in first 3 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 7 12 5 16 19 23 32 37 46 75 86 95

Compare A[M-N+2] and A[M-N+3], put in order


3 7 5 12 16 19 23 32 37 46 75 86 95

Compare A[M-N+3] and A[M-N+4], put in order


3 7 5 12 16 19 23 32 37 46 75 86 95

Compare A[M-N+4] and A[M-N+5], put in order


3 7 5 12 16 19 23 32 37 46 75 86 95

© Janice Regan, CMPT 125, 2023 50


Sample Bubble Sort: 12
Iteration 9: N=M-8
No changes in first 3 comparisons then continue with
A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12]
3 7 5 12 16 19 23 32 37 46 75 86 95
Compare A[M-N+1] and A[M-N+2], put in order
3 5 7 12 16 19 23 32 37 46 75 86 95

Compare A[M-N+2] and A[M-N+3], put in order


3 5 7 12 16 19 23 32 37 46 75 86 95

Compare A[M-N+3] and A[M-N+4], put in order


3 5 7 12 16 19 23 32 37 46 75 86 95
All numbers are in order: We are done
© Janice Regan, CMPT 125, 2023 51
Bubble Sort Function
void BubbleSort(int InArray[], int M, int N)
{
int end; int j; int k; int temp; int sorted=FALSE;
end = N;
while(!sorted)
{
sorted = TRUE;
for( k=M; k<end; k++)
{
if(InArray[k] > InArray[k+1] )
{
sorted = FALSE;
temp = InArray[k];
InArray[k] = InArray[k+1];
InArray[k+1] = temp;
}
}
end--;
}
} 52

You might also like