Date post: | 21-Jan-2015 |
Category: |
Engineering |
Upload: | asmhemu |
View: | 161 times |
Download: | 3 times |
SELECTION SORT
1
Submitted by:
1 Arifarina
2 sakil
3 sabuj sarkar
4 Himu
Submitted to:
Mr.Tahzibul Islam
Lecturer,Department of
cse.Dhaka International University.
2
DATA STRUCTURES-CSE 205PRESENTATION
ROAD MAP
1 Definition
2 Example
3 Algorithm
4 Ascending And Dscending order.
6 Mathematical definition
7Question And Answer
3
4
▪The selection sort is a combination of searching and sorting.
▪In selection sort, sorting is done after selecting a particular smallest or largest element from an array and shifted it to a particular location in an array.
▪During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array.
5
▪Let's look at our same table of elements Using a selection sort for ascending order
▪The number of times the sort passes throughthe array is one less than the number of itemsin the array.
6
Selection Sort Example
Sorted Unsorted
23 78 45 8 32 56 Original List
8 78 45 23 32 56 After pass 1
8 23 45 78 32 56 After pass 1
8 23 32 78 45 56 After pass 1
8 23 32 45 78 56 After pass 1
8 23 32 45 56 78 After pass 1
7
Ascending order Algorithm: Selection sort(L,N)
1 Repeat steps 2 to 6 for I=1 to N 2 set min:=I 3 Repeat steps 4 & 5 for J=I+1 to N 4 if list [J]<list [min] then 5 set lin:=J 6 swap (list [I],list [min]) 7 End.
L=List of items.
N=Totel number of items in the list .
8
▪ In the selection sort, the inner loop finds the next smallest value and the outer loop places that value into its proper location.
Array
40 50 60 10 30 20
1-pass
10 50 60 40 30 20
2-pass
10 20 60 40 30 50
3-pass
10 20 30 40 60 50
4-pass
10 20 30 40 60 50
5-pass
10 20 30 40 50 60
9
Dscending order Algorithm:selection sort(L,N)
1 Repeat steps 2 to 6 for I=1 to N2 min:=I3 Repeat steps 4 & 5 for J=I+1 to N4 if list [J]>list [mix] then5 set mix:=J6 swap(list [I],list-[min])7 End
L=list of items
N=Totel number of items in the list .
10
Array
70 50 11 10 40 20
1-pass
70 50 11 10 40 20
2-pass
70 50 11 10 40 20
3-pass
70 50 40 10 11 20
4-pass
70 50 40 20 11 10
5-pass
70 50 40 20 11 10
▪In the selection sort, the inner loop finds the next largest value and the outer loop places that value into its proper location.
11
i:0; j:1 (7-1);min=I;if(arr[j]<arr[min]) {min=j;} finally swap (a[i],a[min])
89 45 68 90 29 34 17» 17| 45 68 90 29 34 89
i:1;j:2 (7-1); min=I;if(arr[j]<arr[min]) {min=j;} finally swap (a[i],a[min])
17|45 68 90 29 34 89»17|29 |68 90 45 34 89
12
i:2;j:3 (7-1); min=I;if(arr[j]<arr[min]) {min=j;}finally swap (a[i],a[min])
17|29 68 90 45 34 89
17|29|34| 90 45 68 89
Complexity is O(n2).
Kitkat
13
* Any query?
* Any question or any point which you doesn’t get?
14
THANK YOU!