Posted By

farstar78 on 06/08/11


Tagged


Versions (?)

sorting


 / Published in: C++
 

A bit stupid, but object oriented simple sort using an array.

  1. /*
  2.  * P:#33
  3.  * Write a programme to sort n numbers.
  4.  */
  5.  
  6. #include <iostream.h>
  7. #include <conio.h>
  8.  
  9. class Array
  10. {
  11. int *a, n, i;
  12. public:
  13. void get(int);
  14. void sort();
  15. void outdata();
  16. };
  17.  
  18. void Array::get(int x)
  19. {
  20. n = x;
  21. a = new int[n];
  22. cout << "Enter " << n << " numbers one by one:\n";
  23. for (i = 0; i < n; i++)
  24. {
  25. cout << "> ";
  26. cin >> a[i];
  27. }
  28. }
  29.  
  30. void Array::sort()
  31. {
  32. int j;
  33. for(i = 0; i < n; i++)
  34. for(j=i+1; j<n; j++)
  35. {
  36. if(a[i] > a[j])
  37. {
  38. int t = a[i];
  39. a[i] = a[j];
  40. a[j] = t;
  41. }
  42. }
  43. }
  44.  
  45. void Array::outdata()
  46. {
  47. for(i=0;i<n;i++)
  48. cout << a[i] << "\n";
  49. }
  50.  
  51. void main()
  52. {
  53. Array ar;
  54. int s;
  55.  
  56. clrscr();
  57. cout << "Enter how many numbers: ";
  58. cin >> s;
  59. ar.get(s);
  60. ar.sort();
  61. ar.outdata();
  62. getch();
  63. }

Report this snippet  

You need to login to post a comment.