Thursday, November 7
Array Sorting with Selection Sort
In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity.
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
For more detail use Wiki... Selection Sort
#include <iostream>
#incl
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
For more detail use Wiki... Selection Sort
#include <iostream>
#incl
ude <conio.h>
using namespace std;//Selection Sort Function
void Ssort(int *a,int length)
{
int temp;
for(int i=length-1;i>0;i--)
{
int first=0;
for(int j=1;j<=i;j++)
if(a[j]>a[first])first=j; temp=a[first];a[first]=a[i];a[i]=temp;}
}
void main()
{
int arr[10];
int temp[10];
arr[0]=100;
arr[1]=33;
arr[2]=43;
arr[3]=26;
arr[4]=46;
arr[5]=88;
arr[6]=52;
arr[7]=17;
arr[8]=53;
arr[9]=77;
for(int i=0;i<10;i++)
{
temp[i]=arr[i];
cout<<arr[i]<<",";
}
//calling selection sort function
Ssort(temp,10);
//Displaying the result.
cout<<endl<<endl;
for(int i=0;i<10;i++)
cout<<temp[i]<<",";
getch();
}
yaar acha page hai, lekin indent nae hua :/
ReplyDeletei didn't even know this page existed :P
ReplyDelete