SayPro

SayPro Sorting using Heap Sort Algorithm

/* Below program is written in C++ language */

void heapsort(int[], int);
void buildheap(int [], int);
void satisfyheap(int [], int, int);

void 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);
 getch();
}

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];
 }
}

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);
 }
}

Please visit our website at www.saypro.online Email: info@sayro.online Call: + 27 (0) 11 071 1903 Email: info@saypro.online Tel: + 27 11 071 1903 WhatsApp: + 27 84 313 7407. Comment below for any questions and feedback. For SayPro Courses, SayPro Jobs, SayPro Community Development, SayPro Products, SayPro Services, SayPro Consulting, and SayPro Advisory visit our website to www.saypro.online

Comments

Leave a Reply