Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a[10],b[10],size;
  6.  
  7. void swap(int *xp, int *yp)
  8. {
  9. int temp = *xp;
  10. *xp = *yp;
  11. *yp = temp;
  12. }
  13.  
  14. void input(int array[], int size)
  15. {
  16. for (int i = 0; i < size; i++)
  17. {
  18. array[i]=rand()%7;
  19. }
  20. }
  21.  
  22. void Traverse(int array[], int size)
  23. {
  24. for (int i = 0; i < size; i++)
  25. {
  26. cout << array[i] << " ";
  27. }
  28. cout << endl;
  29. }
  30.  
  31. void InsertionSort(int array[], int size)
  32. {
  33. for (int a = 1; a < size; a++)
  34. {
  35. int key = array[a];
  36. int j = a - 1;
  37. while (key < array[j] && j >= 0)
  38. {
  39. array[j + 1] = array[j];
  40. j--;
  41. }
  42. array[j + 1] = key;
  43. }
  44. }
  45. void selectionSort(int array[],int size )
  46. {
  47. int i, j, min;
  48.  
  49. for (i = 0; i < size-1; i++)
  50. {
  51. min = i;
  52. for (j = i+1; j < size; j++)
  53. if (b[j] < b[min])
  54. min = j;
  55.  
  56. swap(&b[min], &b[i]);
  57. }
  58. }
  59. int main()
  60. {
  61. /*cout<<"Size:";
  62. cin>>size;
  63. for(int i=0; i<size; i++)
  64. {
  65. a[i]=rand()%5;
  66. }
  67. for(int i=0; i<size; i++)
  68. {
  69. b[i]=rand()%6;
  70. }*/
  71. int p;
  72. cout<<"1 for insertion sort\n2 for selection sort\n0 for termination:"<<endl;
  73. while(1)
  74. {
  75.  
  76. cout<<"Press:";
  77. cin>>p;
  78. if(p==1)
  79. {
  80.  
  81. cout<<"Size:";
  82. cin>>size;
  83. input(a,size);
  84. Traverse(a,size);
  85. InsertionSort(a,size);
  86. Traverse(a,size);
  87. }
  88. else if(p==2)
  89. {
  90.  
  91. cout<<"Size:";
  92. cin>>size;
  93. input(b,size);
  94. Traverse(b,size);
  95. selectionSort(b,size);
  96. Traverse(b,size);
  97. }
  98. else
  99. return 0;
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement