Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Insertion Sort

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

5 comments:

  1. Is this a insertion sort? This is bubble sort man!

    ReplyDelete
  2. #include
    int 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;
    }

    ReplyDelete
  3. snippet is wrong

    ReplyDelete
  4. plz sumbody upload inseartion sort( correct one) bcuz i m not able to understand wat is the purpose of K here
    j for unsorted , i for sorted but k for ?

    ReplyDelete

Search 24 Bytes

Loading...