Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <<<<<<<<<<<<<<<<<<<sort.h>>>>>>>>>>>>>>>>>>>>>>
- #pragma once
- #include <iostream>
- #include <fstream>
- using namespace std;
- void nhapmang(int[], int);
- void xuatmang(int[], int);
- void xuatmang2(int[], int );
- void selection_sort(int[], int);
- void insertion_sort(int[], int);
- void interchange_sort(int [], int);
- <<<<<<<<<<<<<<<<<<<<sort.cpp>>>>>>>>>>>>>>>>>>>
- #include "sort.h"
- #include <fstream>
- using namespace std;
- #define MAX 100
- int main()
- {
- ifstream file;
- file.open("filetext.txt");
- int x[100];
- int n = 0;
- int j = 0;
- while (!file.eof())
- {
- n++;
- file >> x[j];
- j++;
- }
- file.close();
- xuatmang(x, n);
- selection_sort(x, n);
- cout << "sap xep chon la: ";
- xuatmang2(x, n);
- insertion_sort(x, n);
- cout << "sap xep chen la: ";
- xuatmang2(x, n);
- interchange_sort(x, n);
- cout << "sap xep doi truc tiep la: ";
- xuatmang2(x, n);
- system("pause");
- }
- void nhapmang(int a[], int n)
- {
- do
- {
- cout << "nhap so luong mang: ";
- cin >> n;
- if (n<1 || n>MAX)
- {
- cout << "nhap so luong sai , xin moi nhap lai";
- }
- } while (n<1 || n>MAX);
- for (int i = 0; i < n; i++)
- {
- cout << "nhap A[" << i << "]:";
- cin >> a[i];
- }
- }
- void xuatmang(int a[], int n)
- {
- for (int i = 0; i < n-1; i++)
- {
- cout << " " << a[i];
- }
- cout << "\n";
- }
- void xuatmang2(int a[], int n)
- {
- for (int i = 1; i < n; i++)
- {
- cout << " " << a[i];
- }
- cout << "\n";
- }
- void selection_sort(int a[], int n)//sap xep lua chon
- {
- for (int i = 0; i <n-1; i++)
- {
- int min = i;
- for (int j = i+1; j < n; j++)
- {
- if (a[j]<a[min])
- {
- min = j;
- }
- swap(a[min], a[i]);
- }
- }
- }
- void insertion_sort(int a[], int n)// sap xep chen
- {
- for (int i = 1; i < n; i++)
- {
- int x = a[i];
- int j = i;
- while (j > 0 && a[j - 1] > x)
- {
- a[j] = a[j - i];
- j--;
- }
- a[j] = x;
- }
- }
- void interchange_sort(int a[], int n)// sap xep doi truc tiep
- {
- for (int i = 0; i < n-1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (a[i] > a[j])
- {
- swap(a[i], a[j]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement