Advertisement
hadiuzzaman65

insertion selection bubble

Apr 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int *insertion(int ar[],int n)
  5. {
  6.     for(int i=1; i<n; i++)
  7.     {
  8.         int small=ar[i];
  9.         int j=i-1;
  10.         while(j>=0&&ar[j]>small)
  11.         {
  12.             ar[j+1]=ar[j];
  13.             j--;
  14.  
  15.         }
  16.         ar[j+1]=small;
  17.     }
  18.     return ar;
  19. }
  20. int *selectionsort(int ar[],int n)
  21. {
  22.     for(int i=0; i<n; i++)
  23.     {
  24.         int small=i;
  25.         for(int j=i+1; j<n+1; j++)
  26.         {
  27.             if(ar[j]<ar[small])
  28.                 small=j;
  29.         }
  30.         if(small !=i)
  31.         {
  32.             int temp;
  33.             temp=ar[i];
  34.             ar[i]=ar[small];
  35.             ar[small]=temp;
  36.         }
  37.  
  38.     }
  39.     return ar;
  40. }
  41. int *bubblesort(int ar[],int n)
  42. {
  43.  for(int i=0; i<n; i++)
  44.  {
  45.      for(int j=0; j<n-i-1; j++)
  46.      {
  47.          if(ar[j]>ar[j+1])
  48.          {
  49.              int temp;
  50.              temp=ar[j];
  51.              ar[j]=ar[j+1];
  52.              ar[j+1]=temp;
  53.          }
  54.      }
  55.  }
  56.  return ar;
  57. }
  58. int main()
  59. {
  60.     int ar[]={2,1,6,4,8,9,27,56,30};
  61.     int n=sizeof(ar)/sizeof(int);
  62.     int *p=insertion(ar,n);
  63.     for(int i=0; i<n; i++)
  64.         cout<< p[i]<< " ";
  65.     cout<<endl;
  66.     int *q=selectionsort(ar,n);
  67.     for(int i=0; i<n; i++)
  68.         cout<< q[i]<< " ";
  69.     cout<<endl;
  70.     int *r=bubblesort(ar,n);
  71.     for(int i=0; i<n; i++)
  72.         cout<< r[i]<< " ";
  73.     cout<<endl;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement