One family of internal sorting algorithms is selection sort. The basic idea of selection sort is to repeatedly select the smallest key in the remaining unsorted array. Simply put, an element is taken as the smallest key element
and then it is compared with the other elements to sort the array. It is implemented in the form of exchange selection sort that requires a single array to work with. In this technique, computer keeps on finding the next smallest element and brings it at its appropriate positions.
Selection sort can be used to sort an array in both ascending as well as descending order. The following program demonstrates
the implementation of selection sort in ascending order :
// Program to implement selection sort in C++ ( ascending order )
#include <iostream>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
} // end of sub loop
} // end of main loop
} // end of function selectionSort
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
std::cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
std::cin>>size;
std::cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : ";
std::cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
std::cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
} // end of main
------ OUTPUT ------
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
------ Related Posts ------
Bubble Sort in C++ : http://programsplusplus.blogspot.in/2012/02/bubble-sort-in-c.html
Insertion Sort in C++ : http://programsplusplus.blogspot.in/2012/02/insertion-sort-in-c.html
and then it is compared with the other elements to sort the array. It is implemented in the form of exchange selection sort that requires a single array to work with. In this technique, computer keeps on finding the next smallest element and brings it at its appropriate positions.
Selection sort can be used to sort an array in both ascending as well as descending order. The following program demonstrates
the implementation of selection sort in ascending order :
// Program to implement selection sort in C++ ( ascending order )
#include <iostream>
void selectionSort(int AR[],int size)
{
int i,j,temp;
for (i=0; i<size; i++)
{
for (j=i+1; j<=size; j++)
{
if ( AR[i] > AR[j] ) // sorts in ascending order
{
temp = AR[j];
AR[j] = AR[i];
AR[i] = temp;
}
} // end of sub loop
} // end of main loop
} // end of function selectionSort
int main()
{
int arr[20],size;
// prompts the user to enter the elements in the array
std::cout<<" Enter the no. of elements that you want to enter (max 20 ) : ";
std::cin>>size;
std::cout<<" Now enter the elements in the array ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : ";
std::cin>>arr[i];
}
selectionSort(arr,size); // calls the function to sort the array
std::cout<<" \n The sorted array is as follows ";
for (int i=0; i<size; i++)
{
std::cout<<" \n Element "<<i<<" : "<<arr[i];
}
return 0;
} // end of main
------ OUTPUT ------
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
------ Related Posts ------
Bubble Sort in C++ : http://programsplusplus.blogspot.in/2012/02/bubble-sort-in-c.html
Insertion Sort in C++ : http://programsplusplus.blogspot.in/2012/02/insertion-sort-in-c.html
I have some web hits.
ReplyDelete9.149.68.190 - - [05/Mar/2011:08:38:43 +0000] "GET /honelink/jsp/index.jsp HTTP/1.1" 200 13839 "CLIENT-IP=-" "-"
9.77.156.69 - neidig@us.ibm.com [05/Mar/2011:08:39:33 +0000] "GET /publications/servlet/pbi.wss?CTY=US&FNC=PBL&PBL=SG24-7833-00 HTTP/1.1" 200 24916 "CLIENT-IP=-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
In this way i have many web hits i would like to sort these web hits using Ip address.Could u please help me out in this
Well Kalyan, you see the problem with sorting IP addresses is that, they can vary by a high degree, e.g. one hit can be from 9.149.xx.xxx and another can be from 10.184.xx.xxx. So i suggest you GOOGLE a bit, here are a few links to get point you in the right direction :
ReplyDeletehttp://www.perlmonks.org/?node=Sorting%20IP%20Addresses%20Quickly
http://www.sqlmag.com/article/tsql3/sorting-ip-addresses
you have made the pgming techniques simpler thank u :):):)
ReplyDeleteThnxx....:)
Deletesir it is good but it will be more understandable if you summarize the source code under the program to learners like me
ReplyDeleteif i want to add the descending how?
ReplyDeletethis has a flaw.. If the size of the array is 5. it means you can only index from 0 to 4. but in the code it tries to retrieve AR[5] which will give you a funny number already stored in the memory.
ReplyDeletewen i try diz its not running .
ReplyDeletethis is bubble sort...not selection sort..
ReplyDeleteInsertion Sort in C
ReplyDeleteInsertion Sort is a simplest array data or data Sorting algorithm which sorts the array elements by shifting elements one by one and inserting each element into its proper position.