Subject: Algorithm
need this urgent please.
2.1
1.
code
solution
//output
//copyable code
1.
#include<iostream>
//standard input and output
using namespace std;
void swap(int array1[],int i1,int j1)
{
array1[i1]+=array1[j1];
array1[j1]=array1[i1]-array1[j1];
array1[i1]-=array1[j1];
}
void max_heap(int data[],int heap_Size,int index_val)
{
int max=index_val;
int left=2*index_val+1;
int right=2*index_val+2;
if(left < heap_Size && data[left] > data[max])
{
max=left;
}
if(right < heap_Size && data[right] > data[max])
{
max=right;
}
if(max!=index_val)
{
swap(data[max],data[index_val]);
max_heap(data,heap_Size,max);
}
}
void heap_sort(int data[], int num)
{
for (int i1 = num / 2 - 1; i1 >= 0; i1--)
max_heap(data, num, i1);
for (int i1=num-1; i1>=0; i1--)
{
swap(data[0],data[i1]);
max_heap(data, i1,0);
}
}
int main()
{
int array1[]={7,3,9,3,2,5,6,1,8};
int array_size=9;
int index_val=0;
heap_sort(array1,array_size);
do{
cout<<array1[index_val];
index_val++;
}while(index_val<array_size && cout<<",");
}
//output
//copyable code
#include <bits/stdc++.h>
using namespace std;
//merge function
void merge(int arrvalue[], int startvalue, int midvalue, int endvalue)
{
int startvalue2 = midvalue + 1;
// sorted
if (arrvalue[midvalue] <= arrvalue[startvalue2]) {
return;
}
while (startvalue <= midvalue && startvalue2 <= endvalue) {
if (arrvalue[startvalue] <= arrvalue[startvalue2]) {
startvalue++;
}
else {
int value = arrvalue[startvalue2];
int index = startvalue2;
while (index != startvalue) {
arrvalue[index] = arrvalue[index - 1];
index--;
}
arrvalue[startvalue] = value;
startvalue++;
midvalue++;
startvalue2++;
}
}
}
void merge_Sort(int arrvalue[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
merge_Sort(arrvalue, l, m);
merge_Sort(arrvalue, m + 1, r);
merge(arrvalue, l, m, r);
}
}
void display(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arrvalue[] = {7,3,9,3,2,5,6,1,8};
int arrvalue_size = sizeof(arrvalue) / sizeof(arrvalue[0]);
merge_Sort(arrvalue, 0, arrvalue_size - 1);
display(arrvalue, arrvalue_size);
return 0;
}
Subject: Algorithm need this urgent please. 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A 17, 3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array 3. What...
2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...
please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array AlL..n) that contains every number between 1 and n +1 in...
Subject: Algorithm. solve only part 3 and 4 please. 2.2 Selection- 5 points each 1. Run the simultaneous min-and-max algorithm on the array A 4, 2, 12, 6, 13,9,15). (16, 7, 10, 1,5, 11,3,8, 14, 2. Explain why the above algorithm is better than the naive algorithm for finding minimum and maximum separately. How many comparisons does the naive algorithm do? How many comparisons does the simultaneous min and max do? 3. Use the randomized select algorithm based on partition...