Advertisement
llvlleo1810

SẮP Xếp CHÈN( Insertion sort )

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