| 24
DataSorting
▪ Why we need to sort data?
▪ Applications of sorting
Data Sorting Algorithms
▪ Selection sort
▪ Bubble sort
▪ Insertion sort
▪ Merge sort
▪ Time Complexity, Space complexity and stability of a sorting algorithm
Data Sorting Algorithms
2
3.
| 24
Sortingis a common problem e.g.,
▪ Sorting takes an unordered collection of data and converts it into an ordered
collection. The order may be:
• Ascending order (numerically or lexicographically)
• Descending order (numerically or lexicographically)
Data Sorting Algorithms
3
4.
| 24
Sortingis a common problem e.g.,
▪ 25~50% of all computing power is used for sorting activities.
▪ Sort a list of values, starting from lowest to highest
▪ List of exam scores
▪ Words of dictionary in alphabetical order
▪ Students' names listed alphabetically
▪ Student records sorted by ID
▪ …
Generally, we are given a list of records that have keys
▪ Keys are used to define an ordering of the items in the list
Data Sorting Algorithms
4
5.
| 24
Weneed to sort data to:
▪ Make faster operations on ADTs
• Searching, deleting, insertion etc.
▪ Remove duplicate data
▪ Create validation rules
▪ Consolidate data from multiple sources
▪ What is the importance of sorting in real-life?
• Think of an English word you don’t know, look for it in a dictionary; you’ll get the
answer
Data Sorting Algorithms
5
6.
| 24
SortingAlgorithms
▪ Sorting algorithms are essential for arranging elements in a specific order.
• The order may be ascending or descending.
▪ The efficiency of these algorithms is determined by following two factors:
• Time complexity and space complexity.
Some well-known sort algorithms:
▪ Selection sort
▪ Insertion sort
▪ Bubble sort
▪ Merge sort
▪ Quick sort (very efficient method for most applications)
Data Sorting Algorithms
6
| 24
Intuition
▪Selecting the first value and swapping it with the smallest value
Pseudocode
SelectionSort
{
for all the values in array
select the first value
swap it by the smallest value
select the second value from unsorted part
swap it by the smallest value of unsorted part
repeat until array is sorted
}
Data Sorting Algorithms
8
9.
| 24
1. Selectthe first value (assume smallest)
2. Swap it by the smallest value (if exists)
3. Select the smallest value from unsorted part
4. Swap it by first value of unsorted part
5. Repeat steps 3 and 4 until sorted array
Data Sorting Algorithms
9
| 24
Intuition
▪Repeatedly insert element i with the one to its left, if smaller
Pseudocode
InsertionSort
{
for all the values in array
select second value, compare it with previous value
insert it at correct position if smaller
select next value, compare with the previous values
insert next at correct position if smaller
repeat until array is sorted
}
Data Sorting Algorithms
11
12.
| 24
1. Selectsecond value, compare with previous
2. Insert it at correct position, if smaller
3. Select next value, compare with all previous
4. Insert next at correct position, if smaller
5. Repeat steps 3 and 4 until sorted array
Data Sorting Algorithms
12
| 24
Intuition
▪Compare the values from left to right, swap if necessary
Pseudocode
BubbleSort
{
for all the values in array
if first value is greater than second value
swap second value with first value
repeat until array is sorted
}
Data Sorting Algorithms
14
15.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat steps 3 and 4 until sorted array
Data Sorting Algorithms
15
16.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat 3 and 4 until sorted array
Data Sorting Algorithms
16
17.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat steps 3 and 4 until sorted array
Data Sorting Algorithms
17
18.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat 3 and 4 until sorted array
Data Sorting Algorithms
18
19.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat steps 3 and 4 until sorted array
Data Sorting Algorithms
19
20.
| 24
1. Selectfirst pair of values
2. Swap if first is greater than second
3. Select next pair of values
4. Swap if first is greater than second
5. Repeat 3 and 4 until sorted array
Data Sorting Algorithms
20
21.
| 24
TimeComplexity
▪ Time complexity refers to the time taken by an algorithm to complete its execution
with respect to the size of the input.
▪ It can be represented in different forms:
• Big-O notation (O)
• Omega notation (Ω)
• Theta notation (Θ)
Data Sorting Algorithms
21
22.
| 24
SpaceComplexity
▪ Space complexity refers to the total amount of memory used by the algorithm for a
complete execution.
• It includes both the auxiliary memory and the input.
Stability of an algorithm
▪ A sorting algorithm is considered stable if the two or more items with the same value
maintain the same relative positions even after sorting.
Data Sorting Algorithms
22
| 24
Data SortingAlgorithms
24
You are Welcome
Questions ?
Comments !
Suggestions !!
Data Sorting
▪ Why we need to sort data?
▪ Applications of sorting
Data Sorting Algorithms
▪ Selection sort
▪ Bubble sort
▪ Insertion sort
▪ Merge sort
▪ Time Complexity, Space complexity and stability of a sorting algorithm