<< Chapter < Page | Chapter >> Page > |
int main()
{
int array[9] = {4, 3, 5, 1, 2, 0, 7, 9, 6}; //The Original Array
desc(array, 9);
*array = ins_sort(array, 9);
cout<<"Array Sorted Press Enter To Continue and See the Resultant Array"<<endl
<<"-----------------8<-------------------------------->8--------------";
getchar();
desc(array, 9);
getchar();
return 0;
}
int ins_sort(int* array, int len)
{
for (int i = 0; i<len; i++)
{
int val = array[i];
int key = i;
cout<<"key(Key) = "<<key<<"\tval(Value) = "<<val<<endl;
for (; key>= 1&&array[key-1]>= val; --key)
{
cout<<"Swapping Backward\tfrom (key) "<<key<<" of (Value) "<<array[key]<<"\tto (key) "<<key-1
<<" of (Value) "<<array[key-1];
cout<<"\n\t"<<key<<"<---->"<<key-1<<"\t( "<<array[key]<<"<---->"<<array[key-1]<<" )";
swap(array[key], array[key-1]);
desc(array, 9);
}
}
return *array;
}
bool swap(int&pos1, int&pos2)
{
int tmp = pos1;
pos1 = pos2;
pos2 = tmp;
return true;
}
void desc(int* ar, int len)
{
cout<<endl<<"Describing The Given Array"<<endl;
for (int i = 0; i<len; i++)
cout<<"_______"<<"\t";
cout<<endl;
for (int i = 0; i<len; i++)
cout<<" | "<<i<<" | "<<"\t";
cout<<endl;
for (int i = 0; i<len; i++)
cout<<" ( "<<ar[i]<<" ) "<<"\t";
cout<<endl;
for (int i = 0; i<len; i++)
cout<<"-------"<<"\t";
getchar();
}
Python Example:
def insertion_sort(A):
for i in range(1, len(A)):
key = A[i]
j = i-1
while(j>= 0 and A[j]>key):
A[j+1] = A[j]
j = j-1
A[j+1] = key
(From Wikipedia, the free encyclopedia)
Selection sort is a sorting algorithm , specifically an in-place comparison sort . It has Θ (n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort . Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. It works as follows:
Effectively, we divide the list into two parts: the sublist of items already sorted, which we build up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.
Here is an example of this sort algorithm sorting five elements:
31 25 12 22 11
11 25 12 22 31
11 12 25 22 31
11 12 22 25 31
Selection sort can also be used on list structures that make add and remove efficient, such as a linked list . In this case it's more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far. For example:
31 25 12 22 11
11 31 25 12 22
11 12 31 25 22
11 12 22 31 25
11 12 22 25 31
The following is a C/C++ implementation, which makes use of a swap function:
void selectionSort(int a[], int size)
{
int i, j, min;
for (i = 0; i<size - 1; i++)
{
min = i;
for (j = i+1; j<size; j++)
{
if (a[j]<a[min])
{
min = j;
}
}
swap(a[i], a[min]);
}
}
Python example:
def selection_sort(A):
for i in range(0, len(A)-1):
min = A[i]
pos = i
for j in range(i+1, len(A)):
if( A[j]<min ):
min = A[j]
pos = j
A[pos] = A[i]
Notification Switch
Would you like to follow the 'Data structures and algorithms' conversation and receive update notifications?