ion of All Sorting
-
Upload
abhishek-kumar-singh -
Category
Documents
-
view
215 -
download
0
Transcript of ion of All Sorting
-
8/14/2019 ion of All Sorting
1/16
TTECHNIQUESECHNIQUESssortingorting
By
Sukanta behera
Reg. No. 07SBSCA048
-
8/14/2019 ion of All Sorting
2/16
Topic overview:Topic overview:
NOTES ON SORTING.NOTES ON SORTING.
SELECTION SORT ALGORITHM ANDSELECTION SORT ALGORITHM AND
EXAMPLE.EXAMPLE.
BUBBLE SORT ALGORITHM ANDBUBBLE SORT ALGORITHM AND
EXAMPLE.EXAMPLE.
MERGE SORT ALGORITHM ANDMERGE SORT ALGORITHM AND
EXAMPLE.EXAMPLE.
QUICK SORT ALGORITHM AND EXAMPLE.QUICK SORT ALGORITHM AND EXAMPLE.
-
8/14/2019 ion of All Sorting
3/16
SSORTING:ORTING:
The sorting technique asks us to rearrangeThe sorting technique asks us to rearrange
the given items of a given list in ascendingthe given items of a given list in ascending
order.order.
As a practical manner, we usually need toAs a practical manner, we usually need to
sort lists of numbers, characters from ansort lists of numbers, characters from an
alphabet, character strings, and mostalphabet, character strings, and most
important records in an institutions,important records in an institutions,
companies etc.companies etc.
For example, we can choose to sort studentFor example, we can choose to sort student
-
8/14/2019 ion of All Sorting
4/16
SSelection sort:election sort: Selection sort and Bubble sorting techniques areSelection sort and Bubble sorting techniques are
coming under Brute Force method, which is acoming under Brute Force method, which is astraightforward approach to solving a problemsstraightforward approach to solving a problemsstatement and definitions of the concepts involved.statement and definitions of the concepts involved.
ALGORITHMALGORITHM SelectionSort(A[o..n-1])SelectionSort(A[o..n-1])
//The algorithm sorts a given array by selection sort//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order//Output: Array A[0..n-1] sorted in ascending order
for i=0 to n-2 dofor i=0 to n-2 do
min=imin=i
for j=i+1 to n-1 dofor j=i+1 to n-1 do
if A[j] < A[min], min=jif A[j] < A[min], min=j
swa A i and A minswa A[i] and A[min]
-
8/14/2019 ion of All Sorting
5/16
-
8/14/2019 ion of All Sorting
6/16
Bubble sort:Bubble sort:
Bubble sort is comes under Brute Force method. TheBubble sort is comes under Brute Force method. The
sorting problem is to compare adjacent elements ofsorting problem is to compare adjacent elements of
the list and exchange them if they are out of order .the list and exchange them if they are out of order .
By doing it repeatedly, we end up bubbling up theBy doing it repeatedly, we end up bubbling up the
largest element to the last position of the list and vicelargest element to the last position of the list and vice
versa.versa.
ALGORITHMALGORITHM BubbleSort(A[0..n-1])BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubblesort//The algorithm sorts array A[0..n-1] by bubblesort
//Input: An array A[0..n-1] of orderable elements//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order//Output: Array A[0..n-1] sorted in ascending order
for i=0 to n-2 dofor i=0 to n-2 do
for j=0 to n-2-I dofor j=0 to n-2-I do
-
8/14/2019 ion of All Sorting
7/16
-
8/14/2019 ion of All Sorting
8/16
-
8/14/2019 ion of All Sorting
9/16
Merge sort:Merge sort: Merge sort is a perfect example of a successful application ofMerge sort is a perfect example of a successful application of
the Divide and Conquer method. It sorts a given array A[0..n-the Divide and Conquer method. It sorts a given array A[0..n-
1] by dividing it into two halves A[0..[n/2]-1] and A[[n/2]n-1] by dividing it into two halves A[0..[n/2]-1] and A[[n/2]n-
1]. Sorting each of them recursively and then merging the two1]. Sorting each of them recursively and then merging the two
smaller sorted arrays into a single sorted one.smaller sorted arrays into a single sorted one.
ALGORITHMALGORITHM Mergesort(A[0..n - 1])Mergesort(A[0..n - 1])
//Sorts array A[0..n - 1] by recursive mergesort//Sorts array A[0..n - 1] by recursive mergesort
//Input: An array A[0..n - 1] of orderable elements//Input: An array A[0..n - 1] of orderable elements
// Output: Array A[0..n - 1] sorted in decreasing order// Output: Array A[0..n - 1] sorted in decreasing order
if n > 1if n > 1copy A[0..[n/2] - 1] to B[0[n/2] - 1]copy A[0..[n/2] - 1] to B[0[n/2] - 1]
copy A[[n/2]..n - 1] to C[0[n/2] - 1]copy A[[n/2]..n - 1] to C[0[n/2] - 1]
Mergesort (B[0..[n/2] - 1])Mergesort (B[0..[n/2] - 1])
Mergesort (C[0..[n/2] - 1])Mergesort (C[0..[n/2] - 1])Mer e B C A
-
8/14/2019 ion of All Sorting
10/16
89 45 68 90 29 34 1712
89 45 68 90 29 34 17 12
8945
6890
2934
1712
89
45
68
90
29
34
17
12
4589
6890
29 34 12 17
45 68 89 90 12 17 29 34
12 17 29 34 45 68 8990
-
8/14/2019 ion of All Sorting
11/16
Quick Sort:Quick Sort: Quick sort is another important sorting algorithm that isQuick sort is another important sorting algorithm that is
based on the Divide and Conquer method. It divides thebased on the Divide and Conquer method. It divides the
input elements according to their value. Specifically, itinput elements according to their value. Specifically, it
rearranges elements of a given array A[0..n 1] to achieverearranges elements of a given array A[0..n 1] to achieve
itsits partition.partition.
ALGORITHMALGORITHM Quicksort(A[lr])Quicksort(A[lr])
//Sorts a subarray by quicksort//Sorts a subarray by quicksort
//Input: A subarray A[lr] of A[0..n 1], defined by its left & right//Input: A subarray A[lr] of A[0..n 1], defined by its left & right
indicesindices
//Output: The subarray A[lr] sorted in nondecreasing order//Output: The subarray A[lr] sorted in nondecreasing order
if l < rif l < r==
-
8/14/2019 ion of All Sorting
12/16
ALGORITHMALGORITHM Partition(A[lr])Partition(A[lr])//Partitions a subarray by using its first element as a pivot//Partitions a subarray by using its first element as a pivot
//Input: A subarray A[l..r] of A[on 1], defined by its left//Input: A subarray A[l..r] of A[on 1], defined by its leftand rightand right
// indices l and r (l < r) // indices l and r (l < r)
//Output: A partition of A[l..r], with the split position returned//Output: A partition of A[l..r], with the split position returnedas thisas this
// functions values // functions values
p = A[l]p = A[l]
i = l; j = r + 1i = l; j = r + 1
repeatrepeat
repeatrepeati = i + 1 until A[i] >= pi = i + 1 until A[i] >= prepeatrepeatj = j - 1 until A[j] = j
swap(A[i], A[j]) //undo last swap when i >= jswap(A[i], A[j]) //undo last swap when i >= jswap(A[l],A[j])swap(A[l],A[j])
-
8/14/2019 ion of All Sorting
13/16
0 1 2 3 4 5 6 7 0 1 20 1 2 3 4 5 6 7 0 1 2
3 4 5 6 73 4 5 6 7
55 ii 3 1 9 8 2 4 j 7 13 1 9 8 2 4 j 7 1 22 33
44
5 3 15 3 1
ii9 8 2 j 4 79 8 2 j 4 7 11
5 3 15 3 1 ii 4 8 2 j 9 74 8 2 j 9 7 33
ijij44
5 3 1 45 3 1 4 ii 8 j 2 9 78 j 2 9 7 ii33jj44
5 3 1 4 j 25 3 1 4 j 2 ii 8 9 78 9 7
44
-
8/14/2019 ion of All Sorting
14/16
Insertion sort:Insertion sort: Insertion sort comes under Decrease and ConquerInsertion sort comes under Decrease and Conquer
method. In this method, we consider an application ofmethod. In this method, we consider an application of
the decrease-by-one technique to sorting an array A[0the decrease-by-one technique to sorting an array A[0n 1].n 1].
ALGORITHMALGORITHM Insertionsort(A[0n 1])Insertionsort(A[0n 1])
//Sorts a given array by insertion sort//Sorts a given array by insertion sort
// Input: An array A[0n 1] of n orderable elements// Input: An array A[0n 1] of n orderable elements
// Output: Array A[0n 1] sorted in nondecreasing order// Output: Array A[0n 1] sorted in nondecreasing order
for i =1 to n 1 dofor i =1 to n 1 do
v = A[i]v = A[i]
j = i 1j = i 1
while j >= 0 and A[j ] > v dowhile j >= 0 and A[j ] > v do
A[j + 1] = A[j]A[j + 1] = A[j]
j = j 1j = j 1
A[j + 1] = vA[j + 1] = v
-
8/14/2019 ion of All Sorting
15/16
-
8/14/2019 ion of All Sorting
16/16