<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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