0% found this document useful (0 votes)
44 views22 pages

Lec-5 Basic Sorting

DSA - Basic Sorting

Uploaded by

wifisystesttest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views22 pages

Lec-5 Basic Sorting

DSA - Basic Sorting

Uploaded by

wifisystesttest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

BASIC SORTING TECHNIQUES

REVISION
Bubble Sort
• Approach:
– Compares the adjacent elements
– Swaps the elements if they are in wrong order.
• begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Bubble Sort
Sort array a= [7, 4, 5, 2] in increasing order
Bubble Sort
• Sort array a= [60, 50, 40, 30, 20, 10]
• P-1 i=1 j <n-i-1
– 50 60 40 30 20 10
– 50 40 60 30 20 10
– 50 40 30 60 20 10
– 50 40 30 20 60 10
– 50 40 30 20 10 60
• P-2 i=2
– 40 50 30 20 10 60
– 40 30 50 20 10 60
– 40 30 20 50 10 60
– 40 30 20 10 50 60
Bubble Sort
• Sort array a= [60, 50, 40, 30, 20, 10]
• 40 30 20 10 50 60
• P-3 i=2
– 30 40 20 10 50 60
– 30 20 40 10 50 60
– 30 20 10 40 50 60
• P4
– 20 30 10 40 50 60
– 20 10 30 40 50 60
• P5
– 10 20 30 40 50 60
Bubble Sort
• Sort array a= [60, 50, 40, 30, 20, 10]
• Analysis:
• N=6 elements:
– 5 (i.e. N-1 ) Passes
– 1st pass: (n-1) comparisons
– 2nd pass: (n-2) comparisons
– 3rd pass: (n-3) comparisons)
– N-1 th pass: (N- (N-1)) comparisons
• Total = N-1 +N-2+N-3+ .... + N-(N-1)
– Or 1+2+3+4+5+6+.....
– N(N+1)/2
– Hence, O(n^2)
Bubble Sort
• Sort array a= [60, 50, 40, 30, 20, 10]
• Analysis:
• N=6 elements:
– 5 (i.e. N-1 ) Passes
– 1st pass: (n-1) comparisons & swaps
– 2nd pass: (n-2) comparisons & swaps
– 3rd pass: (n-3) comparisons & swaps
– N-1 th pass: (N- (N-1)) comparisons
• Total = N-1 +N-2+N-3+ .... + N-(N-1)
– Or 1+2+3+4+5+6+.....
– O(N^2)
– Hence, O(n^2) [Both comparison & swaps]
Bubble Sort
• Sort array a= [10, 20, 40, 30, 60, 50]
• 10 20 30 40 50 60 P-1
• 10 20 30 40 50 60 P-2
• 10 20 30 40 50 60 P-3
• P4 P5
Bubble Sort
• Sort array a= [10, 20, 40, 30, 60, 50]
• Modified:
• We use a flag
• For i 0 to n-1
– For j 0 to n-i-1
• { swap
• Flag variable-change status
•}
– Check status
– If status not changed then stop
–}
Bubble Sort
• Sort array a= [10, 20, 40, 30, 60, 50]
• Analysis:
– Single pass with 2 swaps was sufficient.
– Still O(n^2)comparisons will be made unnecessarily.
– Hence, still O(n^2).
• Solution:
– Optimize it
– Stop when no swaps are made during a pass.
– Can make use of a ‘flag’ variable.
– Best case: O(n)
– Space: O(1)
– Stable sorting
Bubble Sort
• Sort array a= [10, 20, 40, 30, 60, 50]
• Analysis of optimized solution:
– Best case: O(n)
• N comparisons will be made only
– Worst case: O(n^2)
• This won’t change
Selection Sort
• Approach
– Find the minimum or maximum element in an
unsorted array
– Put it in its correct position in a sorted array.

– Step 1 − Set MIN to location 0


– Step 2 − Search the minimum element in the list
– Step 3 − Swap with value at location MIN
– Step 4 − Increment MIN to point to next element
– Step 5 − Repeat until list is sorted
Selection Sort
• Approach
Selection Sort
• Sort a=[10, 60, 40, 50, 20, 30]
• Min=0 i=0 mv=10
• P1
– 10 60 40 50 20 30
• P2
– Min=1 i=1 mv=20
– 10 20 40 50 60 30
• P3
– Min=2 mv=40
– 10 20 30 50 60 40
Selection Sort
• Sort a=[10, 60, 40, 50, 20, 30]
• P410 20 30 40 60 50
• P5 10 20 30 40 50 60
Selection Sort
• Sort a=[10, 60, 40, 50, 20, 30]
• Analysis: 5 Passes i.e. (n-1) passes.
– 1st pass: n-1 comp and 0 swap
– 2nd pass: n-2 comp and 1 swap
– 3rd pass: n-3 comp and 1 swap
– Nth pass: n-(n-1) comp and 1 swap
• Total: (n-1)+(n-2)+(n-3)+........1
– =O(n^2)
– 1 swap for each pass. Hence, O(n) swaps
– Not Stable sorting
– Best : O(n^2).
– Space: O(1)
Insertion Sort
• Approach:
– It considers part of a list sorted and rest of the list as
unsorted.
– Then continuously picks elements from the unsorted
part and puts them at appropriate position in the
sorted list by shifting the elements to right.
Insertion Sort
• Approach:
– INSERTION-SORT(A)
for i = 0 to n-1
temp ← A [i]
j←i
while j > 0 and A[j-1] >temp
A[j] ← A[j-1]
j←j–1
End while
A[j] ← temp
End for
Insertion Sort
Insertion Sort
• Sort array {50 10, 30, 80,70, 20, 60}
– P1 temp=10 j=1 i=1
– 10 50 30 80 70 20 60
– P2 i=2, temp=30, j=2
– 10 30 50 80 70 20 60
– P3 i=3 temp=80
– 10 30 50 80 70 20 60
– Temp=70
– 10 30 50 70 80 20 60
– Temp=20
Insertion Sort
• Sort array {10,30, 80,70, 20, 60}
10 30 50 70 80 20 60
Temp=20 i=5 j=5
10 30 50 70 80 80 60
10 30 50 70 70 80 60
10 30 50 50 70 80 60
10 30 30 50 70 80 60
10 20 30 50 70 80 60

P6:
10 20 30 50 70 80 80
10 20 30 50 70 70 80
10 20 30 50 60 70 80
Insertion Sort
• Analysis:
• Passes= (n-1)
• Comparisons depend upon N
• Best case: O(N)
• Worst case:O(N^2)
• Fewer swaps
• In-place.
• Best suited and preferred for nearly / almost
sorted arrays due to best case complexity as O(n)
and fewer swaps

You might also like