Wednesday, January 20, 2016

გროვებით დალაგება (heap sort) C++

 #include <iostream>
using namespace std;
void heapsort(int[], int);
void buildheap(int [], int);
void satisfyheap(int [], int, int);

int main()
{
  int a[10], i, size;
  cout << "Enter size of list  ";    // less than 10, because max size of array is 10
  cin >> size;
  cout << "Enter " << size << "  elements ";
  for( i=0; i < size; i++)
  {
    cin >> a[i];
  }
  heapsort(a, size);
 system("pause");
}

void heapsort(int a[], int length)
{
  buildheap(a, length);
  int heapsize, i, temp;
  heapsize = length - 1;
  for( i=heapsize; i >= 0; i--)
  {
    temp = a[0];
    a[0] = a[heapsize];
    a[heapsize] = temp;
    heapsize--;
    satisfyheap(a, 0, heapsize);
  }
  for( i=0; i < length; i++)
  {
    cout << "\t" << a[i];
  }
   cout << "\n";
}

void buildheap(int a[], int length)
{
  int i, heapsize;
  heapsize = length - 1;
  for( i=(length/2); i >= 0; i--)
  {
    satisfyheap(a, i, heapsize);
  }
}

void satisfyheap(int a[], int i, int heapsize)
{
  int l, r, largest, temp;
  l = 2*i;
  r = 2*i + 1;
  if(l <= heapsize && a[l] > a[i])
  {
    largest = l;
  }
  else
  {
    largest = i;
  }
  if( r <= heapsize && a[r] > a[largest])
  {
    largest = r;
  }
  if(largest != i)
  {
    temp = a[i];
    a[i] = a[largest];
    a[largest] = temp;
    satisfyheap(a, largest, heapsize);
  }
}


მუშაობის პრინციპი:
https://www.youtube.com/watch?v=EreoMaOBTzE

გაკვეთილები:
https://www.dropbox.com/s/hc2hijc3tythdz1/grovebit%20dalageba.docx?dl=0
https://www.dropbox.com/s/dy3oq69o6trjqe7/grovebit%20dalageba2.docx?dl=0

 პრეზენტაციის ლინკი:
 https://www.dropbox.com/s/xgxgtyhmdj2m8id/108133.pptx?dl=0





No comments:

Post a Comment