Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Sorting Algorithms

Uploaded by

yashasarda27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Sorting Algorithms

Uploaded by

yashasarda27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT – iV

sorting

•Basic searching in an array of elements (linear


and binary search techniques),
•Basic algorithms to sort array of elements
(Bubble, Insertion and Selection
sort ,Merge ,Quick algorithms),
•Basic concept of order of complexity through
the example programs
Bubble Sort
Bubble sort algorithm
1)Start at index zero, compare the element with the next one (a[0] &
a[1] (a is the name of the array)), and swap if a[0] > a[1]. Now
compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process
until the end of the array. After doing this, the largest element is
present at the end. This whole thing is known as a pass. In the first
pass, we process array elements from [0,n-1].
2)Repeat step one but process array elements [0, n-2] because the last
one, i.e., a[n-1], is present at its correct position. After this step, the
largest two elements are present at the end.
3)Repeat this process n-1 times.
Example
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since
5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
logic
for(i=1;i<n;++i)
for(j=0;j<n-i-1;++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
Selection Sort
• The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning.
• The algorithm maintains two sub arrays in a given array.
1) The sub array which is already sorted.
2) Remaining sub array which is unsorted.
• In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted sub array is picked
and moved to the sorted sub array.
Example
arr[] = 64 25 12 22 11
pass1:
Find the minimum element in arr[0...4] 64 25 12 22 11
and place it at beginning 11 25 12 22 64
Pass2:
Find the minimum element in arr[1...4] 25 12 22 64
and place it at beginning of arr[1...4] 12 25 22 64
Pass3:
Find the minimum element in arr[2...4] 25 22 64
and place it at beginning of arr[2...4] 22 25 64
Pass4:
Find the minimum element in arr[3...4] 25 64
and place it at beginning of arr[3...4] 25 64
Final sorted array is 11 12 22 25 64
N elements n-1 passes
Logic
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;
// Swap the found minimum element with the first element
swap(arr[min],arr[i]);
}
Insertion Sort
• Insertion sort is a simple sorting algorithm that works similar to the way
you sort playing cards in your hands.
• The array is virtually split into a sorted and an unsorted part. Values from
the unsorted part are picked and placed at the correct position in the
sorted part.

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the
elements before. Move the greater elements one position up to make
space for the swapped element.
Example:
Another Example: 12, 11, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 4 (last element of the
array)
i = 1.
Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
i = 2.
13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
i = 3.
5 will move to the beginning and all other elements from 11 to 13 will move
one position ahead of their current position.
5, 11, 12, 13, 6
i = 4.
6 will move to position after 5, and elements from 11 to 13 will move one
position ahead of their current position.
5, 6, 11, 12, 13
Logic
for(i=1;i<count;i++)
{
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0))
{
number[j+1]=number[j];
j=j-1; }
number[j+1]=temp;
}
MERGE SORT
Merge Sort Approach
• To sort an array A[p . . r]:
• Divide
– Divide the n-element sequence to be sorted into
two subsequences of n/2 elements each
• Conquer
– Sort the subsequences recursively using merge sort
– When the size of the sequences is 1 there is
nothing more to do
• Combine
– Merge the two sorted subsequences
13
Merge Sort
p q r
1 2 3 4 5 6 7 8

Alg.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6

if p < r Check for base case

then q ← ⎣(p + r)/2⎦ Divide


MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine

• Initial call: MERGE-SORT(A, 1, n)

14
Example – n Power of 2
1 2 3 4 5 6 7 8

Divide 5 2 4 7 1 3 2 6 q=4

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

15
logic
1. MERGE_SORT(arr, beg, end)
2.

3. if beg < end


4. set mid = (beg + end)/2
5. MERGE_SORT(arr, beg, mid)
6. MERGE_SORT(arr, mid + 1, end)
7. MERGE (arr, beg, mid, end)
8. end of if
9.

10. END MERGE_SORT


Quicksort A[p…q] A[q+1…r]

• Sort an array A[p…r]


• Divide
– Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such
that each element of A[p..q] is smaller than or equal to each
element in A[q+1..r]
– Need to find index q to partition the array

17
Quicksort A[p…q] A[q+1…r]

• Conquer
– Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
– Trivial: the arrays are sorted in place
– No additional work is required to combine them
– The entire array is now sorted

18
QUICKSORT
Initially: p=1, r=n
Alg.: QUICKSORT(A, p, r)

if p < r

then q ← PARTITION(A, p, r)

QUICKSORT (A, p, q)

QUICKSORT (A, q+1, r)


Recurrence:
PARTITION())
T(n) = T(q) + T(n – q) + f(n)
19
Partitioning the Array
• Choosing PARTITION()
– There are different ways to do this
– Each has its own advantages/disadvantages
• Hoare partition (see prob. 7-1, page 159)
– Select a pivot element x around which to partition
A[p…i] ≤ x x ≤ A[j…r]
– Grows two regions
A[p…i] ≤ x
x ≤ A[j…r]
i j
20
Example
A[p…r] pivot x=5

5 3 2 6 4 1 3 7 5 3 2 6 4 1 3 7

i j i j

3 3 2 6 4 1 5 7 3 3 2 6 4 1 5 7

i j i j

A[p…q] A[q+1…r]

3 3 2 1 4 6 5 7 3 3 2 1 4 6 5 7

i j j i

21
Example

22
Partitioning the Array
Alg. PARTITION (A, p, r)
r
1. x ← A[p]
p

A: 5 3 2 6 4 1 3 7
2. i ← p – 1
3. j ← r + 1 i j
4. while TRUE A[p…q] ≤ A[q+1…r]

5. do repeat j ← j – 1
A: ap ar
6. until A[j] ≤ x
7. do repeat i ← i + 1 j=q i

8. until A[i] ≥ x
9. if i < j Each element is
visited once!
10. then exchange A[i] ↔ A[j]
Running time: Θ(n)
11. else return j n=r–p+1

23
Time complexity of sortings

Algorithm Time Complexity

Best Average Worst

Selection Sort Ω(n^2) θ(n^2) O(n^2)

Bubble Sort Ω(n) θ(n^2) O(n^2)

Insertion Sort Ω(n) θ(n^2) O(n^2)

Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)

Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n)


Thank you

You might also like