Frooster

Sorting Simulation of selection sort

Oct 11th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include "head.h"
  2. #define SIZE 10
  3. #define FPS 10
  4.  
  5. void creditsScroll()
  6. {
  7.  clrscr();
  8.   for(int o=24;o>=1;o--)
  9.   {
  10.    delay(30);
  11.    gotoxy(30,o);
  12.    cout<<"     Thank You";
  13.    gotoxy(30,o+1);
  14.    cout<<"Program by Frooster";
  15.    gotoxy(30,o+2);
  16.    cout<<"       XII A";
  17.    delay(30);
  18.    clrscr();
  19.   }
  20. }
  21.  
  22. void stepAnim(int k)
  23. {
  24.  gotoxy(1,3);
  25.  cout<<"\t\t\tSELECTION SORT USING ANIMATION"<<endl;
  26.  cout<<"\t\t\t==============================";
  27.  gotoxy(37,9);
  28.  cout<<"STEP "<<k;
  29.  gotoxy(37,10);
  30.  cout<<"~~~~~~";
  31.  if(k==10)
  32.   cout<<"~";
  33. }
  34.  
  35. int returnPos(int x)
  36. {
  37.  switch(x)
  38.  {
  39.   case 1: return 1;
  40.   case 2: return 9;
  41.   case 3: return 17;
  42.   case 4: return 25;
  43.   case 5: return 33;
  44.   case 6: return 41;
  45.   case 7: return 49;
  46.   case 8: return 57;
  47.   case 9: return 65;
  48.   case 10: return 73;
  49.  }
  50. }
  51.  
  52. void swap(int &x,int &y)
  53. {
  54.  int temp;
  55.  temp=x;
  56.  x=y;
  57.  y=temp;
  58. }
  59.  
  60. void main()
  61. {
  62.  //Declarations
  63.  int a[SIZE];//={7,9,4,3,1,0,2,5,8,6}; <--Remove comment if you dont want to input array and comment "Array Input"
  64.  int csize=10,cstart=0,cpos;
  65.  clrscr();
  66.  
  67.  //Array Input
  68.  cout<<"Enter an array:\n";
  69.  for(int i=0;i<SIZE;i++)
  70.   cin>>a[i];
  71.  
  72.  
  73.  //Array Output
  74.  clrscr();
  75.  gotoxy(1,14);
  76.  for(int j=0;j<SIZE;j++)
  77.   cout<<a[j]<<"\t";
  78.  
  79.  //Animation
  80.  for(int k=1;k<=SIZE;k++)
  81.  {
  82.   stepAnim(k);
  83.   int cmin=99,l=0;
  84.   for(l=cstart;l<=csize;l++)
  85.   {
  86.    if(a[l]<cmin)
  87.    {
  88.     cmin=a[l];
  89.     cpos=l;
  90.    }
  91.   }
  92.   cpos++;
  93.  
  94.   //Move Up-Down
  95.   getch();
  96.   gotoxy(returnPos(cpos),14);
  97.   cout<<" ";
  98.   delay(FPS);
  99.   gotoxy(returnPos(cpos),13);
  100.   cout<<cmin;
  101.   delay(FPS);
  102.   gotoxy(returnPos(cstart+1),14);
  103.   cout<<" ";
  104.   delay(FPS);
  105.   gotoxy(returnPos(cstart+1),15);
  106.   cout<<a[cstart];
  107.   delay(FPS);
  108.  
  109.   //Move Left-Right
  110.   for(int q=0;q<=returnPos(cpos)-1;q++)
  111.   {
  112.    gotoxy(cstart+q,15);
  113.    cout<<" ";
  114.    delay(FPS);
  115.    gotoxy(cstart+q+1,15);
  116.    cout<<a[cstart];
  117.    delay(FPS);
  118.    gotoxy(returnPos(cpos)-q,13);
  119.    cout<<" ";
  120.    delay(FPS);
  121.    gotoxy(returnPos(cpos)-q-1,13);
  122.    cout<<cmin;
  123.    delay(FPS);
  124.   }
  125.  
  126.   //Move Up-Down
  127.   gotoxy(cstart+1,13);
  128.   cout<<" ";
  129.   delay(FPS);
  130.   gotoxy(cstart+1,14);
  131.   cout<<cmin;
  132.   delay(FPS);
  133.   gotoxy(returnPos(cpos),15);
  134.   cout<<" ";
  135.   delay(FPS);
  136.   gotoxy(returnPos(cpos),14);
  137.   cout<<a[cstart];
  138.  
  139.   /*Cleanup
  140.   for(int s=1;s<=70;s++)
  141.   {
  142.    gotoxy(s,13);
  143.    cout<<" ";
  144.    gotoxy(s,15);               <--Use to clean the output..like remove the stranded nos etc...bugged
  145.    cout<<" ";
  146.    gotoxy(s,16);
  147.    cout<<" ";
  148.   }
  149.   */
  150.   delay(FPS);
  151.  
  152.   //Actual Swap
  153.   swap(a[cstart],a[cpos-1]);
  154.   cstart++;
  155.  }
  156.  getch();
  157.  creditsScroll();
  158.  delay(2000);
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment