- #include<iostream>
- using namespace std;
- void insertion(int a[], int n)
- {
- for(int i=1; i<n; i++)
- {
- int ele= a[i];
- int j=i-1;
- while(a[j]>ele && j>=0)
- {
- a[j+1]=a[j];
- --j;
- }
- a[j+1]= ele;
- }
- }
- void selection(int a[], int n)
- {
- 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 merge(int a[], int left, int mid, int right)
- {
- int a1= mid-left+1, a2= right-mid;
- int templeft[a1], tempright[a2];
- for(int i=0; i<a1; i++)
- {
- templeft[i]= a[i+left];
- }
- for(int i=0; i<a2; i++)
- {
- tempright[i]= a[mid+1+i];
- }
- int idxleft=0, idxright=0, idxarr= left;
- while(idxleft<a2)
- {
- if(templeft[idxleft]<tempright[idxright])
- {
- a[idxarr]= templeft[idxleft];
- idxleft++;
- }
- else
- {
- a[idxarr]= tempright[idxright];
- idxright++;
- }
- idxarr++;
- }
- while(idxleft<a1)
- {
- a[idxarr]= templeft[idxleft];
- idxleft++, idxarr++;
- }
- while(idxright<a2)
- {
- a[idxarr]= tempright[idxright];
- idxright++, idxarr++;
- }
- }
- void mergesort(int a[], int left, int right)
- {
- if(left>=right)
- {
- return;
- }
- int mid= (left+right)/2;
- mergesort(a, left, mid);
- mergesort(a, mid+1, right);
- merge(a, left, mid, right);
- }
- int partition(int a[], int low, int high)
- {
- int pivot= a[high];
- int i= low-1;
- for(int x= low; x<high; x++)
- {
- if(a[x]<pivot)
- {
- i++;
- swap(a[i], a[x]);
- }
- }
- swap(a[i+1], a[high]);
- return i+1;
- }
- void quicksort(int a[], int low, int high)
- {
- if(low>=high)
- return;
- int pi= partition(a, low, high);
- quicksort(a, low, pi-1);
- quicksort(a, pi+1, high);
- }
- void heapify(int a[], int n, int i)
- {
- int largest=i;
- int l= 2*i+1;
- int r= 2*i+2;
- if(l>a[largest])
- {
- largest=l;
- }
- if(r>a[largest])
- {
- largest=r;
- }
- if(largest != i)
- {
- swap(a[largest], a[i]);
- heapify(a, n, largest);
- }
- }
- void heapsort(int a[], int n)
- {
- for(int i=n/2-1; i>=0; i--)
- {
- heapify(a, n, i);
- }
- for(int i= n-1; i>0; i--)
- {
- swap(a[0], a[i]);
- heapify(a, i, 0);
- }
- }
- int main()
- {
- int n;
- cin>>n;
- int a[n];
- for(int i=0; i<n; i++)
- {
- cin>>a[i];
- }
- heapsort(a, n);
- for(int x=0; x<n; x++)
- {
- cout<<a[x]<<" ";
- }
- }