Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <iostream>
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
temp[k] = a[i];
k++;
i++;
else
temp[k] = a[j];
k++;
j++;
temp[k] = a[i];
k++;
i++;
temp[k] = a[j];
k++;
j++;
a[i] = temp[i-low];
int mid;
mid=(low+high)/2;
int main()
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
cin>>arr[i];
MergeSort(arr, 0, n-1);
cout<<"->"<<arr[i];
return 0;
The basic algorithm MERGE is the procedure of combining two sorted arrays into a new sorted array.
Now we will have two pointers first pointing at the first position of Arr1 and the other on the 1st
position of the Arr2.
Then they go on comparing the values of the two pointers. The pointer with lesser value copy the
value to the Arr3 and move a place right
.
Now for the first comparison Arr1[0]=1 and Arr2[0]=2. So as 1<2 hence Arr3[0]=1 and the pointer at
Arr1[0] incements and now points towards Arr1[1].
Similarly now Arr1[1]=5 and Arr2[0]=2. We know that 2<5 and hence Arr3[1]=Arr2[0]=2. Pointer
pointing towards Arr2[0] increments and now pointing towards Arr2[1].
Similarly now Arr1[1]=5 and Arr2[1]=4. We know that 4<5 and hence Arr3[2]=Arr2[1]=4. Pointer
pointing towards Arr2[1] increments and now pointing towards Arr2[2].
#include <iostream>
int largest = i;
largest = l;
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
int startIdx = (n / 2) - 1;
heapify(arr, n, i);
int main()
{
int arr[] = { 1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17 };
buildHeap(arr, n);
printHeap(arr, n);
return 0;
/ \
3 5
/ \ / \
4 6 13 10
/\ /\
9 8 15 17
/ \
3 5
/ \ / \
4 17 13 10
/\ / \
9 8 15 6
/ \
3 5
/ \ / \
9 17 13 10
/\ / \
4 8 15 6
/ \
3 13
/ \ / \
9 17 5 10
/\ / \
4 8 15 6
/ \
17 13
/ \ / \
9 15 5 10
/\ / \
4 8 3 6
17
/ \
15 13
/ \ / \
9 6 5 10
/\ / \
4 8 3 1