Sorting Algorithm
Sorting Algorithm
Prepared by:
▫ [ 1, 2, 2, 3, 4, 5, 5, 5, 6 ]
• In Place Sort
▫ Suppose a set of data to be sorted is stored in an array A
▫ If a sorting method takes place within the array A only, i.e.
without using any other extra storage space
▫ It is a memory efficient sorting method
Stability
• Stable sorting algorithms maintain the relative order of
records with equal keys.
▫ A key is that portion of record which is the basis for the sort
▫ it may or may not include all of the record
• If all keys are different then this distinction is not
necessary.
• But if there are equal keys, then a sorting algorithm is
stable if whenever there are two records (let's say R
and
S) with the same key, and R appears before S in the
original list, then R will always appear before S in
the sorted list.
Stability
• When equal elements are indistinguishable, such as
with integers, or more generally, any data where the
entire element is the key, stability is not an issue.
• However, assume that the following pairs of numbers are
to be sorted by their first component:
• (4, 2) (3, 7) (3, 1) (5, 6)
• Two different results are possible,
▫ one which maintains the relative order of records with
equal keys, and one which does not:
• (3, 7) (3, 1) (4, 2) (5, 6) (order maintained)
• (3, 1) (3, 7) (4, 2) (5, 6) (order changed)
Sorting Methods
Swap? 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Yes! 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Swap? 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
0
No. [1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Swap? 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
No. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Swap? 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Yes! 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Swap? 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• The Bubble Sort
algorithm looks at
pairs of entries in 70
the array, and 60
swaps their order
if needed. 50
40
30
20
10
Yes! 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? No. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? No. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Repea
t.
70
60
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• Repeat.
70
60
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Loop over array
n-1 times,
swapping pairs 70
of entries as 60
needed.
50
40
30
20
10
Swap? No. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Loop over array
n-1 times,
swapping pairs 70
of entries as 60
needed.
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Loop over array
n-1 times,
swapping pairs 70
of entries as 60
needed.
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• Loop over array n-1
times, swapping
pairs of entries as 70
needed. 60
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Loop over array n-1
times, swapping pairs
of entries as needed. 70
60
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm
• Continue looping, until
done.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• Continue looping, until
done.
70
60
50
40
30
20
10
Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Bubble Sort Algorithm
• Continue looping, until
done.
70
60
50
40
30
20
10
Swap? Yes. 0
[0] [1] [2] [3] [4] [5]
Bubble Sort Algorithm - Result
70
60
50
40
30
20
10
0
[0] [1] [2] [3] [4] [5]
Bubble Sort Implementation
void bubbleSort (int list[ ] , int
size) { int i, j, temp;
for ( i = 0; i < size; i++ ) { /* controls passes through the
list */
for ( j = 0; j < size - 1; j++ ) /* performs adjacent
comparisons */
{
if ( list[ j ] > list[ j+1 ] ) /* determines if a swap should
occur */
{
temp = list[ j ]; /* swap is performed
*/ list[ j ] = list[ j + 1 ];
list[ j+1 ] = temp;
} // end of if statement
} // end of inner for loop
} // end of outer for loop
} // end of function
Performance
• Worst and Average Case Time Complexity:
O(n*n). Worst case occurs when array is reverse
sorted.
• Best Case Time Complexity: O(n). Best case
occurs when array is already sorted.
• Auxiliary Space: O(1)
• Sorting In Place: Yes
• Stable: Yes
Summary
• Introduction to Sorting Algorithms
• Bubble Sort Algorithm, Algorithm Analysis
References
• https://www.geeksforgeeks.org/sorting-
algorithms/
• https://brilliant.org/wiki/sorting-algorithms/
• https://betterexplained.com/articles/sorting-
algorithms/
• https://codeburst.io/algorithms-i-searching-
and-sorting-algorithms-56497dbaef20