fork download
  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4.  
  5. int * selectionSort(int* A, int n)
  6. {
  7. for(int i=0;i<n-1;i++)
  8. {
  9. int minInde = i;
  10. for(int j = i +1; j<n;j++)
  11. {
  12. if(A[minInde]>A[j])
  13. minInde = j;
  14. }
  15. int temp = A[i];
  16. A[i] = A[minInde];
  17. A[minInde] = temp;
  18. }
  19. return A;
  20. }
  21. void print(int*A,int n)
  22. {
  23. for(int i=0;i<n;i++)
  24. {
  25. cout<<A[i]<<" ";
  26. }
  27. cout<<endl;
  28. }
  29. int main()
  30. {
  31. int i;
  32. int A[5];
  33. srand(static_cast<unsigned int>(time(0)));
  34. for(i=0;i<5;i++)
  35. {
  36. int random = rand()%5;
  37. A[i] = random;
  38. }
  39. cout<<"RANDOMLY GERERATED NUMBERS\n--------"<<endl;
  40. cout<<"BEFORE THE SELECTION SORT ALGORITHM: "<<endl;
  41. print(A,5);
  42. cout<<"AFTER THE SELECTION SORT ALGROTITHM"<<endl;
  43. selectionSort(A,5);
  44. print(A,5);
  45. /*cout<<"-------"<<endl;
  46.   cout<<"CASE A(The most number of swaps"<<endl;
  47.  
  48.   cout<<"CASE BThe least number of swaps"<<endl;
  49.   int C[5] = {1,2,3,4,5};
  50.   cout<<"This contains the least number of swaps since it is already sorted"<<endl;*/
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 4480KB
stdin
Standard input is empty
stdout
RANDOMLY GERERATED NUMBERS
--------
BEFORE THE SELECTION SORT ALGORITHM: 
2 3 2 0 0 
AFTER THE SELECTION SORT ALGROTITHM
0 0 2 2 3