Sorting
Prepared by:
ENGR. VANESH KUMAR
Lecturer, Computer Science Deptt:
Thar Institute of Engineering, Sciences & Technology
Last Lecture Summary
• Overview of Search Algorithms
• Algorithm Analysis
• Time and Space Complexity
• Big O Notation
• Introduction of Linear Searching
• Introduction to Binary Search,
• Comparison of Linear and Binary
Search
Objectives Overview
• Introduction to Sorting Algorithms
• Bubble Sort Algorithm, Algorithm Analysis
Sorting
Sorting
• A fundamental operation in computer science
• Task of rearranging data in an order such as
▫ Ascending
▫ Descending
▫ Lexicographic
• Data may be of any type like numeric,
alphabetical or alphanumeric
• It also refers to rearranging a set of records
based on their key values when the records are
stored in a file
• Sorting task arises more frequently in the world of
data manipulation
Sorting
• Sorting and searching frequently apply to a
file of records
• Each record in a file F can contain many fields but
there may be one particular field whose values
uniquely determine the records in the file called
primary key
• The key or key values are k1, k2, ……
• Sorting the file F usually refers to sorting F with
respect to a particular primary key
• Searching in F refers to searching for a record
with a given key value
Sorting
• Let A be a list of n elements in memory
▫ A1, A2, ……., An
• Sorting refers to the operations of rearranging the
contents of A so that they are increasing in order
numerically or lexicographically so that
▫ A1 A2 A3 ………….. An
• Since A has n elements, there are n! ways that contents
can appear in A
• These ways correspond precisely to the n! permutations
of 1, 2, …., n
• Accordingly each sorting algorithms must take care of
these n! possibilities
Basic Terminology
• Internal sort
▫ When a set of data to be sorted is small enough
such that the entire sorting can be performed in a
computer’s internal storage (primary memory)
• External sort
▫ Sorting a large set of data which is stored in low
speed computer’s external memory such as hard
disk, magnetic tape, etc.
• Ascending order
▫ An arrangement of data if it satisfies “less than or
equal to <=“ relation between two consecutive data
▫ [1, 2, 3, 4, 5, 6, 7, 8, 9]
Basic Terminology
• Descending order
▫ An arrangement of data if it satisfies “greater than or equal
to
>=“ relation between two consecutive data
▫ e.g. [ 9, 8, 7, 6, 5, 4, 3, 2, 1]
• Lexicographic order
▫ If the data are in the form of character or string of characters
and are arranged in the same order as in dictionary
▫ e.g. [ada, bat, cat, mat, max, may, min]
• Collating sequence
▫ Ordering for a set of characters that determines whether a
character is in higher, lower or same order compared to
another
▫ e.g. alphanumeric characters are compared according to
their ASCII code
▫ e.g. [AmaZon, amaZon, amazon, amazon1, amazon2]
Basic Terminology
• Random order
▫ If a data in a list do not follow any ordering mentioned above, then it
is arranged in random order
▫ e.g. [8, 6, 5, 9, 3, 1, 4, 7, 2]
▫ [may, bat, ada, cat, mat, max, min]
• Swap
▫ Swap between two data storages implies the interchange of their
contents.
▫ e.g. Before swap A[1] = 11, A[5] = 99
▫ After swap A[1] = 99, A[5] = 11
• Item
▫ Is a data or element in the list to be sorted.
▫ May be an integer, string of characters, a record etc.
▫ Also alternatively termed key, data, element etc.
Basic Terminology
• Stable Sort
▫ A list of data may contain two or more equal data. If a
sorting method maintains the same relative position of
their occurrences in the sorted list then it is stable sort
▫ e.g. [ 2, 5, 6, 4, 3, 2, 5, 1, 5 ]
▫ [ 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
Bubble Sort Heap Sort
Selection Sort Merge Sort
Insertion Sort Quick Sort
Bubble
Sort
Bubble Sort
• Sometimes incorrectly referred to as sinking sort, is a
simple sorting algorithm that works by repeatedly
stepping through the list to be sorted, comparing each
pair of adjacent items and swapping them if they are in
the wrong order.
• The pass through the list is repeated until no swaps
are needed, which indicates that the list is sorted.
• The algorithm gets its name from the way smaller
elements "bubble" to the top of the list.
• Because it only uses comparisons to operate
on elements, it is a comparison sort.
Bubble Sort
• Bubble sort is a simple sorting algorithm
• The algorithm starts at the beginning of the data set.
▫ It compares the first two elements, and if the first is
greater than the second, it swaps them.
▫ It continues doing this for each pair of adjacent
elements to the end of the data set.
▫ It then starts again with the first two elements,
repeating until no swaps have occurred on the last
pass.
• Note that the largest end gets sorted first, with
smaller elements taking longer to move to their
correct positions.
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
[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
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