Advertisement
llvlleo1810

sắp xếp chọn ( selection sort) --Đề bài : sắp xếp theo thuật

Apr 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. bool doctep(int a[100], int &n){
  6.  ifstream doc("output.txt");
  7.  if(doc.fail()){
  8.   return false;
  9.  }
  10.  doc >> n;
  11.  for(int i= 0; i <n; i++){
  12.   doc >> a[i];
  13.  }
  14.  return true;
  15. }
  16.  
  17. void doicho(int &x, int & y){
  18.  int tmp = x;
  19.   x = y;
  20.   y = tmp;
  21. }
  22. //4 !5 2 8 3
  23. //2 5 !4 8 3
  24. //2 3 4 !8 5
  25. //2 3 4 8 !5
  26. //2 3 4 5 8
  27. void sapxepchon(int a[100], int n){
  28.  int csMin;
  29.  for(int i = 0; i < n-1; i++){
  30.   csMin = i;
  31.   for(int j = i+1; j < n; j++){
  32.    if(a[j] < a[csMin]){
  33.     csMin = j;
  34.    }
  35.   }
  36.   if (csMin != i) {
  37.    doicho(a[i], a[csMin]);
  38.   }
  39.  }
  40. }
  41.  
  42. void ghitep(int a[100], int n){
  43.  ofstream ghi("sxepchon.txt");
  44.  ghi << "sap xep chon la: ";
  45.  for(int i = 0 ; i < n; i++) {
  46.   ghi << a[i] << " ";
  47.  }
  48. }
  49.  
  50. int main() {
  51.  int a[100];
  52.  int n;
  53.  if(doctep(a,n)) {
  54.   sapxepchon(a,n);
  55.   ghitep(a,n);
  56.  }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement