0% found this document useful (0 votes)
7 views

Selection&merge Sort

Uploaded by

Fiza Abrar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Selection&merge Sort

Uploaded by

Fiza Abrar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

PRESENTATION TOPIC:

SORTING

Presented By: Maria Amber


13171556-017
BS(IT)3rd
SORTING
 The process of arranging data in a specified order
according to a given criteria is called sorting.

 The numeric type data may be arranged either in


ascending or in descending order.

 Character type data may be arranged in alphabetical


order.
SORTING
 Sorting methods can be divided into two types based upon the complexity
of their algorithms.

 One type of sorting algorithms includes


 Bubble Sort
 Insertion Sort
 Selection Sort

 Other type consists is


 Merge Sort
SELECTION SORT
 It is simple and easy to implement

 It is inefficient for large list, usually used to sort lists of


no more than 1000 items

 In array of n elements, n-1 iterations are required to sort


the array
SELECTION SORT
 It process to sort an array in ascending order consists of the
following iterations:

 In first iteration, the array is scanned from the first to the last element
and the element that has the smallest is selected. The value of the
selected smallest element is interchanged with the first element of the
array

 In second iteration, the array is scanned from second to the last element
and the element that has smallest value is selected. The value of
smallest element is interchanged with the second element of the array

 This process is repeated until the entire array is sorted


SELECTION SORT
 Suppose the name of the array is A and it has four elements
with the following values:

4 19 1 3

 To sort this array in ascending order, n-1, i.e.


three iterations will be required.
SELECTION SORT

4 19 1 3
 Iteration-1
The array is scanned starting from the first to the last element and
the element that has the smallest value is selected. The smallest
value is 1 at location 3. The address of element that has the smallest
value is noted and the selected value is interchanged with the first
element i.e.
A[1] and A[3] are swapped

1 19 4 3
SELECTION SORT

1 19 4 3
 Iteration-2
The array is scanned starting from the second to the last element and
the element that has the smallest value is selected. The smallest
value is 3 at location 4. The address of element that has the smallest
value is noted. The selected value is interchanged with the second
element i.e.
A[2] and A[4] are swapped

1 3 4 19
SELECTION SORT

1 3 4 19
 Iteration-3
The array is scanned starting from the third to the last element and
the element that has the smallest value is selected. The smallest
value is 4 at location 3. The address of element that has the smallest
value is noted. The selected value is interchanged with the third
element i.e.
A[3] and A[3] are swapped

1 3 4 19
ALGORITHM – SELECTION SORT
SelectionSort()
Algorithm to sort an array A consisting of N elements in
ascending order
1. Start

2. Set U = 1

3. Repeat step 4 to 11 While (U<N)

4. Set Temp = A[U]

5. Set Loc = U

6. Set I = U + 1

7. Repeat Step 8 to 9 While (I<=N)


ALGORITHM – SELECTION SORT
8. If Temp > A[I] then
Temp = A[I]
Loc = I
End if
9. I = I + 1

10. T = A[Loc]

A[Loc] = A[U]
A[U] = T
11. U = U + 1

12. Exit
MERGE SORT
MERGE SORT
 It splits the list to be sorted into two equal halves and
places them in separate array

 Each array is recursively sorted and then merged back


together to form the final sorted list

 It requires more memory than other sorting methods


because it requires use of an additional array
MERGE SORT
To sort an array:
 Split the array into two arrays

 Sort the left half

 Sort the right half

 Merge the two halves into the final sorted array


MERGE SORT
 The array is split into two halves by finding the midpoint of the
array

 If the lower bound of the array is LB and upper bound is UB, then
the midpoint of array is:
MID = (LB + UB) / 2

 First sub-array has lower bound LB and upper bound MID

 Second sub-array has lower bound MID + 1 and upper bound UB


MERGE SORT
 Suppose the name of the array is AB and it has six elements
with the following values:

16 17 2 8 18 1

 To sort this array in ascending order


MERGE SORT
AB
16 17 2 8 18 1

 Divide array AB into two sub-arrays A & B

A B
16 17 2 8 18 1

 Sort A & B using Bubble or selection or insertion sort


A B
2 16 17 1 8 18
MERGE SORT
A B
2 16 17 1 8 18

 Compare A[1] to B[1], so B[1] is less than A[1], the value of B[1] is
move to AB[1]

AB
1
MERGE SORT
A B
2 16 17 1 8 18

 Compare A[1] to B[2], so A[1] is less than B[2], the value of A[2] is
move to AB[2]

AB
1 2
MERGE SORT
A B
2 16 17 1 8 18

 Compare A[2] to B[2], so B[2] is less than A[2], the value of B[2] is
move to AB[3]

AB
1 2 8
MERGE SORT
A B
2 16 17 1 8 18

 Compare A[2] to B[3], so A[2] is less than B[3], the value of A[2] is
move to AB[4]

AB
1 2 8 16
MERGE SORT
A B
2 16 17 1 8 18

 Compare A[3] to B[3], so A[3] is less than B[3], the value of A[3] is
move to AB[5]
AB
1 2 8 16 17

 At the end, B[3] is move to AB[6], array is sorted


AB

1 2 8 16 17 18
ALGORITHM – MERGE SORT
MergeSort()
Algorithm to sort an array AB consisting of N elements
1. Start
2. Divide array AB into two sub-arrays A & B
3. Set Mid = (N + 1) / 2
4. Repeat step-5 For I = 1 to Mid by 1
5. A[I] = AB[I]
6. Repeat step-7 For I = Mid + 1 to N by 1
7. B[I-Mid] = AB[I]
8. Set R = Mid
9. Set S = N - Mid
10. Sort A & B
ALGORITHM – MERGE SORT
11. Set L1 = 1
12. Set L2 = 1
13. Set L = 1
14. Repeat Step-15 While (L1 <= R and L2 <= S)
15. If A[L1] < B[L2] then
AB[L] = A[L1]
L=L+1
L1 = L1 + 1
Else
AB[L] = B[L2]
L=L+1
L2 = L2 + 1
End if
ALGORITHM – MERGE SORT
16. If L1 > R then
Repeat For I = 0 to S - L2
AB[L+I] = B[L2+I]
Else
Repeat For I = 0 to R - L1
AB[L+I] = A[L1+I]
End if
17. Exit
Keep Calm
It is the
End of My
Presentation

You might also like