write a program that calculates the number of comparisons in binary insertion sorting? - sorting

I would like to know how I can write a program to calculate the number of comparison in binary insertion sorting
I tried to code the binary insertion program below, but I would like to know how I can calculate the overall comparisons made.
#include<iostream>
using namespace std;
int binarysearch (int a[], int sel, int high, int low){
int mid=(high+low)/2;
if(high<=low){
if(sel>a[high]){
return high+1;
}
else{
return high;
}
}
else{
if(sel==a[mid]){
return mid+1;
}
else if(sel>a[mid]){
return binarysearch( a, sel, high, mid+1);
}
else{
return binarysearch( a, sel, mid-1, low);
}
}}
void insertionsort(int a[], int n){
for(int i=1; i<n; i++){
int j=i-1;
int sel=a[i];
int loc=binarysearch(a,sel,j,0);
while(j>=loc){
a[j+1]=a[j];
j--;
}
a[j+1]=sel;
}
}
int main(){
int a[]= {1,6,2,5,3,4};
int n=sizeof(a)/sizeof(a[0]);
insertionsort(a,n);
cout<<"Sorted array is :";
for (int i = 0; i < n; i++)
cout<<a[i]<<"\t";
return 0;
}

Related

Time and space complexity of determinant of a matrix

Can anybody please explain the calculation of time and space complexity of this code(finding determinant of a matrix) in detail?
I am not able to understand the exact time complexity of this program using recurrence tree.
class Solution
{
static void getCofactor(int arr[][], int temp[][], int n, int i, int j){
int x = 0, y = 0;
for(int row=0; row<n; row++){
for(int col=0; col<n; col++){
if(row!=i && col!=j){
temp[x][y++] = arr[row][col];
if(y == n-1){
y = 0;
x++;
}
}
}
}
}
static int determinantOfMatrix(int matrix[][], int n)
{
// code here
if(n == 1)
return matrix[0][0];
int temp[][] = new int[n-1][n-1];
int determinant = 0;
int sign = 1;
for(int i=0; i<n; i++){
getCofactor(matrix, temp, n, 0, i);
determinant+=sign*matrix[0][i]*determinantOfMatrix(temp, n-1);
sign=-sign;
}
return determinant;
}
}

Is it normal for quicksort to be inefficient when sorting a completely descending array? [duplicate]

This question already has answers here:
Quick sort Worst case
(6 answers)
What is the worst case scenario for quicksort?
(6 answers)
Closed 4 years ago.
#include <iostream>
#include<stdio.h>
#include<fstream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
int arr[100000];
int i;
ifstream fin;
int n = 20000;
fin.open("reverse20k.txt");
if(fin.is_open())
{
for(i=0;i<n;i++)
fin>>arr[i];
}
quickSort(arr, 0, n-1);
return 0;
}
It takes this about 1.25 seconds to sort a 20k purely descending array, while it takes merge sort only 0.05. Is quick sort just extremely inefficient when sorting descending arrays, or is there just something wrong with the algorithm?

Geeksforgeeks is showing my code as runtime error after submission

This is my code
#include <bits/stdc++.h>
using namespace std;
int srch(vector<int> arr, int ln, int fn)
{
for (int i = 1; i <= ln; i++)
{
if (arr[i] == fn)
return i;
}
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
vector<int> a(n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%d\n", srch(a, n, k));
}
return 0;
}
I am not understanding where is the problem. Here is the problem link Click here.
Please help me solving this problem. I am not understanding why geeksforgeeks is showing runtime error for this code.
Note that your loops use n-th entry of vector, but
vector< int> a(n);
has indexes from 0 to n-1
Notice that the first element has a position of 0 (not 1).

Why is this code not working? Output is not being printed

When I compiled in devc++ compiler, output is not printed. Is there any logical error? This program should accept a number from the user, print the lowest palindrome greater than the given number.
It is a problem from SPOJ PALIN
include
using namespace std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{long long int i;
long long int k;
int flag=1;
long long int n;
int a[10000000];
cin>>n;
n++;
start:
i=0;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;}
i--;
k=i;
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{flag=0;break;}
else
i--;
}
if(flag==1)
cout<<n<<endl;
else
{n++;goto start;}
}
return 0;
}
You have not initialized the flag variable. if(a[i]==a[j]) then the flag will remain uninitialized and may create problems while cheking the if condition unless the default value will be 0 for it.(I am not sure that it would be). Also I can see that the value of flag will never turn 1 according to your code.
#include<iostream>
using namespace::std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{
long int i, k, n; int a[1000]; int flag = 1, duplicaten;
cin>>n;
n++;
start:
//cout<<"inside start : n = "<<n<<"\n";
flag=1; i=0;
duplicaten=n;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;
}
n=duplicaten;
i--;
k=i;
//cout<<"number of digits of n = "<<i+1<<"\n";
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
i--;
}
//cout<<"flag = "<<flag<<endl;
if(flag==1)
{
//cout<<"FOUND!!\n";
cout<<n<<endl;
}
else
{
n++;
//cout<<"NOT found\n";
goto start;
}
}
return 0;
}
You can change the size of your data types yourself and atleast vote up this answer.
< Output before using duplicaten : link to image >

Weighted Activity Selection

I am stuck with this problem for a few days-
Consider a modification to the activity-selection problem in which each activity ai has, in addition to a start and finish time, a value vi. The objective is no longer to maximize the number of activities scheduled, but instead to maximize the total value of the activities scheduled. That is, we wish to choose a set A of compatible activities such that summation of their corresponding values is maximized. Give a polynomial-time algorithm for
this problem.
I know this question has been answered here & I have devised a DP approach to find out the summation of the values. But can you print the activities that were selected?
The C++ code is given below. Hope you can easily translate it to any other language.
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 1000
struct activity
{
int start;
int finish;
int weight;
};
bool comp(activity m, activity n)
{
return m.finish>n.finish;
}
int dp[N];
int main()
{
int n, maxi=-1;
int DP[N];
activity a[N];
scanf("%d", &n);
for(int i=0;i<n;i++)
{
scanf("%d %d %d", &a[i].start, &a[i].finish, &a[i].weight);
a[i].pos=i+1;
}
sort(a, a+n, comp);
for(int i=0; i<n; i++)
dp[i]=a[i].weight;
for(int i=1; i<n; i++)
{
for(int j=0; j<i; j++)
{
if(a[j].finish<=a[i].start)
dp[i]=max(dp[i], dp[j]+a[i].weight);
maxi=max(dp[i], maxi);
}
}
printf("%d\n", maxi);
int act[N];
int k=0;
for(int i=n-1;i>=0;i--)
{
if(dp[i]==maxi)
{
maxi-=a[i].weight;
act[k++]=a[i].pos;
}
}
for(int i=k-1;i>=0;i--)
printf("%d ", act[i]);
return 0;
}

Resources