Professional Documents
Culture Documents
Sorting
The Process of rearranging the elements so that they are in ascending order or descending order is called sorting. Example: arranging of numbers, student records. very essential for searching the dictionaries, telephone directories.
Properties
The two Main properties of sorting techniques are: Stable: If the sorting algorithm preserves the relative order of any two equal elements, then the sorting algorithm is stable. In Place: If an algorithm does not require an extra memory space except for few memory units, then the algorithm is said to be in place.
Sorting Algorithms
Various sorting algorithms are Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort
Bubble Sort
This is one of the most simplest sorting technique and most straight forward method of sorting. In this sorting technique, the adjacent elements in the list are compared and exchanged if they are out of order. This is also called as Sinking Sort
An Animated Example
N to_do index 8 7 did_swap true
98 1
23 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 1 did_swap false
98 1
23 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 1 Swap did_swap false
98 1
23 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 1 Swap did_swap true
23 1
98 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 2 did_swap true
23 1
98 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 2 Swap did_swap true
23 1
98 2
45 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 2 Swap did_swap true
23 1
45 2
98 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 3 did_swap true
23 1
45 2
98 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 3 Swap did_swap true
23 1
45 2
98 3
14 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 3 Swap did_swap true
23 1
45 2
14 3
98 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 4 did_swap true
23 1
45 2
14 3
98 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 4 Swap did_swap true
23 1
45 2
14 3
98 4
6 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 4 Swap did_swap true
23 1
45 2
14 3
6 4
98 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 5 did_swap true
23 1
45 2
14 3
6 4
98 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 5 Swap did_swap true
23 1
45 2
14 3
6 4
98 5
67 6
33 7
42 8
An Animated Example
N to_do index 8 7 5 Swap did_swap true
23 1
45 2
14 3
6 4
67 5
98 6
33 7
42 8
An Animated Example
N to_do index 8 7 6 did_swap true
23 1
45 2
14 3
6 4
67 5
98 6
33 7
42 8
An Animated Example
N to_do index 8 7 6 Swap did_swap true
23 1
45 2
14 3
6 4
67 5
98 6
33 7
42 8
An Animated Example
N to_do index 8 7 6 Swap did_swap true
23 1
45 2
14 3
6 4
67 5
33 6
98 7
42 8
An Animated Example
N to_do index 8 7 7 did_swap true
23 1
45 2
14 3
6 4
67 5
33 6
98 7
42 8
An Animated Example
N to_do index 8 7 7 Swap did_swap true
23 1
45 2
14 3
6 4
67 5
33 6
98 7
42 8
An Animated Example
N to_do index 8 7 7 Swap did_swap true
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
45 2
14 3
6 4
67 5
33 6
42 7
98 8
23 1
14 2
45 3
6 4
67 5
33 6
42 7
98 8
23 1
14 2
45 3
6 4
67 5
33 6
42 7
98 8
23 1
14 2
45 3
6 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
67 5
33 6
42 7
98 8
23 1
14 2
6 3
45 4
33 5
67 6
42 7
98 8
23 1
14 2
6 3
45 4
33 5
67 6
42 7
98 8
23 1
14 2
6 3
45 4
33 5
67 6
42 7
98 8
23 1
14 2
6 3
45 4
33 5
42 6
67 7
98 8
23 1
14 2
6 3
45 4
33 5
42 6
67 7
98 8
23 1
14 2
6 3
45 4
33 5
42 6
67 7
98 8
23 1
14 2
6 3
45 4
33 5
42 6
67 7
98 8
14 1
23 2
6 3
45 4
33 5
42 6
67 7
98 8
14 1
23 2
6 3
45 4
33 5
42 6
67 7
98 8
14 1
23 2
6 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
45 4
33 5
42 6
67 7
98 8
14 1
6 2
23 3
33 4
45 5
42 6
67 7
98 8
14 1
6 2
23 3
33 4
45 5
42 6
67 7
98 8
14 1
6 2
23 3
33 4
45 5
42 6
67 7
98 8
14 1
6 2
23 3
33 4
42 5
45 6
67 7
98 8
14 1
6 2
23 3
33 4
42 5
45 6
67 7
98 8
14 1
6 2
23 3
33 4
42 5
45 6
67 7
98 8
14 1
6 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
6 1
14 2
23 3
33 4
42 5
45 6
67 7
98 8
Selection Sort
In this technique first we find the smallest item in the list and we exchange it with the first item. Next, obtain the second smallest item in the list & exchange it with the second element and so on. Since, the next least item is selected and exchanged appropriately so that elements are finally sorted ,this technique is called Selection Sort
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
6
Largest
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
5
Largest
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
4
Largest
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
3
Largest
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
1
Comparison Data Movement Sorted
Selection Sort
2
Largest
Selection Sort
2
Comparison Data Movement Sorted
Selection Sort
2
Comparison Data Movement Sorted
3
DONE!
Insertion Sort
In this technique the given list is divided into two parts:sorted part(left) &unsorted part(right). The unsorted elements can be placed any of the positions in the sorted part so that elements towards left of boundary are sorted. As each item is inserted towards the sorted left part, the boundary moves to the right decreasing the unsorted list. Finally,once the boundary moves to the right most position,the elements towards the left of boundary represent the sorted list.
Average case:
Best case:
T(n) = (n^2)
T(n) = (n)
Insertion Sort
23
17
45
18
12
22
Insertion Sort
23
1
17
2
45
3
18
4
12
5
22
6
Insertion Sort
23
1
17
2
45
3
18
4
12
5
22
6
Insertion Sort
17
1 2
45
3
18
4
12
5
22
6
23
1 2
Insertion Sort
45
1 2 3
18
4
12
5
22
6
17
1
23
2
Insertion Sort
18
1 2 3 4
12
5
22
6
17
1
23
2
45
3 4
5 6
Insertion Sort
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
Insertion Sort
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
Insertion Sort
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
Insertion Sort
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
Insertion Sort
12
1
17
2
18
3
22
4
23
5
45
6
Merge sort
In merge sort, a given array of elements is divided into two parts. The left part of the array as well as the right part of the array is sorted recursively. Later, the sorted left part and the sorted right part are finally merged into a single sorted vector. The process of merging of two sorted vectors into a single sorted vector is called simple merge.
Merge Sort
9 2 6 5 3 10 1 7
Merge Sort
9
2 2
10
7 8
Merge Sort
9
10
Merge Sort
9
10
10
Merge Sort
9
10
10
Merge Sort
9
10
10
Merge Sort
9
10
10
Merge Sort
9
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
Merge Sort
9
10
10
10
10
Conclusion
By comparing all the sorting techniques Merge sort time complexity is less in all three cases (time consuming is less). So, Merge sort is the most efficient technique when compare to all other sorting techniques.
THANK YOU