Upcoming SlideShare
×

# Selection sort

6,510 views

Published on

Published in: Technology, Career
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
6,510
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
299
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Selection sort

1. 1.  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. Lets look at our same table of elements using a selection sort for ascending order:
2. 2. 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 The number of times the sort passes through the array is one less than the number of items in the array. In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location. Lets watch a video which will give you a basic idea of a selection sort. Please co-operate.
3. 3.  Lets look at coding of it in c++: #include<iostream> using namespace std; void selectionsort(int *array,int length) { int i,j,min,minat,temp; for(i=0;i<(length-1);i++) { minat=i; min=array[i]; for(j=0;j<length;j++) { if(min>array[j]) { minat=j; min=array[j]; } } temp=array[i]; array[i]=array[minat]; array[minat]=temp; } }
4. 4.  void printelements(int *array,int length) { int i; for(i=0;i<length;i++) cout<<array[i]<<” “<<; }
5. 5.  int main() { int array[100],n,i; cout<<”enter number of element you wants to enter in an array”; cin>>n; if(n<0) { cout<<”enter proper positive element”; cin>>n; } for(i=0;i<n;i++) { cout<<”enter values in array”; cin>>array[i]; } selectionsort(array,n); printelement(array,n); return 0; }
6. 6.  Any query? Any question or any point which you doesn’t get?