code-DS-ALGO icon indicating copy to clipboard operation
code-DS-ALGO copied to clipboard

codes need to be added

Open adityanjr opened this issue 6 years ago • 7 comments
trafficstars

choose the right folder to add your codes..

adityanjr avatar Nov 13 '19 02:11 adityanjr

#include #include <math.h> using namespace std; int heapSize;

void buildMaxHeap(int[],int);

void maxHeapify(int ptr[], int i, int s) { int l = 2i + 1; int r = 2i + 2; int largest = i; if((ptr[l]>ptr[largest]) && l<s) largest = l;

if((ptr[r]>ptr[largest]) && r<s)
    largest = r;

if(largest != i)
{
    swap(ptr[i], ptr[largest]);
    maxHeapify(ptr, largest, s);
}

}

void buildMaxHeap(int ptr[], int s) { for(int i=(s/2)-1; i>=0; i--) maxHeapify(ptr, i, s);

for (int i=s-1; i>=0; i--)
{
	swap(ptr[0], ptr[i]);
	maxHeapify(ptr, 0, i);
}

}

void heapSort(int ptr[], int s) { buildMaxHeap(ptr, s); }

int main() { int * ptr, n; cout << "Enter the size of the Array: "; cin >> n; ptr = new int[n]; if(ptr == NULL) { cout << "Insufficient Memory."; } cout<< "Enter the elements:\n"; for(int i=0; i<n; i++) { cout<<"["<<i<<"]:"; cin >> ptr[i]; } heapSort(ptr, n); cout << "\nSorted Array: \n"; for(int i=0; i<n; i++) { cout << ptr[i] << " "; } return 0; }

vaibh289 avatar Oct 01 '20 17:10 vaibh289

#include #include <math.h> using namespace std; int heapSize;

void buildMaxHeap(int[],int);

void maxHeapify(int ptr[], int i, int s) { int l = 2i + 1; int r = 2i + 2; int largest = i; if((ptr[l]>ptr[largest]) && l<s) largest = l;

if((ptr[r]>ptr[largest]) && r<s)
    largest = r;

if(largest != i)
{
    swap(ptr[i], ptr[largest]);
    maxHeapify(ptr, largest, s);
}

}

void buildMaxHeap(int ptr[], int s) { for(int i=(s/2)-1; i>=0; i--) maxHeapify(ptr, i, s);

for (int i=s-1; i>=0; i--)
{
	swap(ptr[0], ptr[i]);
	maxHeapify(ptr, 0, i);
}

}

void heapSort(int ptr[], int s) { buildMaxHeap(ptr, s); }

int main() { int * ptr, n; cout << "Enter the size of the Array: "; cin >> n; ptr = new int[n]; if(ptr == NULL) { cout << "Insufficient Memory."; } cout<< "Enter the elements:\n"; for(int i=0; i<n; i++) { cout<<"["<<i<<"]:"; cin >> ptr[i]; } heapSort(ptr, n); cout << "\nSorted Array: \n"; for(int i=0; i<n; i++) { cout << ptr[i] << " "; } return 0; }

vaibh289 avatar Oct 01 '20 17:10 vaibh289

add in the repo.

adityanjr avatar Oct 01 '20 22:10 adityanjr

Aditya can I add solutions to binarysearch.io problems. Is this acceptable?

srisrinu1 avatar Oct 17 '20 10:10 srisrinu1

@srisrinu1 Yes, just add them into a seperate folder.

adityanjr avatar Oct 17 '20 11:10 adityanjr

hii @adityanjr can i add some codes on sorting algorithm? kindly assign me the issue then.Thank you

vivek-0110 avatar Oct 13 '21 23:10 vivek-0110

@vivek-0110 Look through the folder and add if it is not already there. just make pull request, i'll review.

adityanjr avatar Oct 13 '21 23:10 adityanjr