Insertion Sort in c++
#include < iostream.h >
#include < conio.h >
void sort(int * a) {
for (int j = 2; j < 10; j++) {
for (int k = 0; k < j; k++) {
if (a[j] < a[k]) {
int temp = a[k];
a[k] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < 10; i++) {
cout << a[i] << "\n";
}
}
void main() {
clrscr();
int a[] = {
1, 4, 6, 8, 0, 9, 7, 5, 2, 3};
sort(a);
}
Insertion Sort in c++
Labels
Insertion Sort
Subscribe to:
Post Comments (Atom)
Is this a insertion sort? This is bubble sort man!
ReplyDelete#include
ReplyDeleteint main()
{
cout<<"How many elements?";
int n;
cin>>n;
int a[100],i,j,k,temp,ptr;
cout<<"Enter elements:";
for(i=1;i<=n;i++)
cin>>a[i];
a[0]=-99;
for(k=2;k<=n;k++)
{
temp=a[k];
ptr=k-1;
if(temp<a[ptr])
{
do
{
a[ptr+1]=a[ptr];
ptr=ptr-1;
}
while(temp<a[ptr]);
}
a[ptr+1]=temp;
}
cout<<"On sorting:";
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
return 0;
}
snippet is wrong
ReplyDeleteplz sumbody upload inseartion sort( correct one) bcuz i m not able to understand wat is the purpose of K here
ReplyDeletej for unsorted , i for sorted but k for ?
code is full of buggggzzzzz
ReplyDelete