Tue Apr 10, 2007 2:13 pm
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
// C++ Insertion Sort Example
char ch='Y';
do
{
// Create New One
int array[50];
// variable carry
int numbersCount;
cout<<"How many numbers you want to sort?- "<<endl;
cin>>numbersCount;
for(int i=0;i<numbersCount;i++){
cout<<"\nGive me the numbers:- ";
cin>>array[i];
}
int tempArray[30];
tempArray[0] = array[0];
// Loop on all numbers
for (int i = 1; i < numbersCount; i++)
{
int temp = array[i];
int j = i - 1;
// insert to the array until find number smaller than current one.
while (( tempArray[j] > temp) && (j>=0))
{
tempArray[j+1] = tempArray[j];
j--;
}
tempArray[j+1] = temp;
}
for (int k = 0; k < numbersCount; k++)
{
array[k] = tempArray[k];
}
cout<<"Insertion sorting is done..."<<endl;
cout<<"The sorted numbers are"<<endl;
for(int z=0;z<numbersCount;z++){
cout<<array[z]<<",";
}
// To Start Again
cout<<"\n Do you want to sort other numbers(Y/N)"<<endl;
ch=getch();
}while(ch=='Y');
}
Sun Dec 16, 2012 9:59 pm
|
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.