Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- void Sink(int A[],int k, int n)
- {
- int placed = 0;
- int j = 2*k+1; //el index is k
- while (placed == 0 && j < (n-1))
- {
- if (j < n && A[j] <= A[j+1])
- {
- j = j + 1;
- }
- if (A[k] >= A[j])
- {
- placed = 1;
- }else{
- swap(A[k], A[j]);
- }
- k = j;
- j = 2*k + 1;
- }
- }
- void HeapSort(int A[], int n)
- {
- for (int i = (n - 1) / 2; i > -1; i--)
- {
- Sink( A, i, n);
- }
- for (int i = n - 1; i > -1; i--)
- {
- swap(A[0], A[i]);
- Sink(A, 0, i-1);
- }
- }
- int main()
- {
- ifstream fin("1.txt");
- ofstream fout("2.txt");
- int n; // number of elmts
- int a[n];
- fin >> n;
- for (int i = 0; i < n; i++)
- {
- fin >> a[i];
- }
- HeapSort(a, n);
- for (int i = 0; i < n; i++)
- {
- cout << a[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement