<< 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

6.1.2. selection sort

(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:

  1. Find the minimum value in the list
  2. Swap it with the value in the first position
  3. Repeat the steps above for remainder of the list (starting at the second position)

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

Implementation

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]

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Data structures and algorithms. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10765/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Data structures and algorithms' conversation and receive update notifications?

Ask