void max_heapify(int* u, int i, int size)
{
//push down the ith element in the max tree
int l = 2*i+1;
int r = 2*i+2;
int largest =i;
if(l<=size && u[l]>u[largest])
largest = l;
else
largest = i;
if(r<=size && u[r]>u[largest])
largest = r;
if(i != largest)
{
//exchange i and largest and call recurse max_heapify
int t=u[i];
u[i] = u[largest];
u[largest] = t;
max_heapify(u, largest, size);
}
}
void build_max_heap(int* u, int size)
{
for(int i=size/2; i>=0; i--)
{
max_heapify(u, i, size);
}
}
void heap_sort(int* u, int size)
{
build_max_heap(u, size);
cout << "max heap: ";
print_arr(size);
int hsize = size;
for(int i=0; i< size; i++)
{
hsize--;
int t= u[0];
u[0] = u[hsize];
u[hsize] = t;
max_heapify(u, 0, hsize-1);
}
}
This blog site is dedicated for articles on problem solving using C, C++, data structures, algorithms. Besides this few technical articles are also presented.
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Apr 19, 2011
sort an array using mergesort
void merge_sort(int* u, int size)
{
if(size==1)
{
return;
}
//divide the array into 2
//sort each
merge_sort(u, size/2);
merge_sort(u+(size/2), size-(size/2));
int* t=(int*)malloc(sizeof(int)*(size));
for(int i=0,anext=0,bnext=0; i< size; i++)
{
if(anext==size/2)
{
t[i] = *(u+(size/2)+bnext);
bnext++;
}
else if(bnext == size-(size/2))
{
t[i] = *(u+anext);
anext++;
}
else
{
if(*(u+anext) < *(u+(size/2)+bnext))
{
t[i] = *(u+anext);
anext++;
}
else
{
t[i] = *(u+(size/2)+bnext);
bnext++;
}
}
}
//copy back to org array
for(int i=0;i< size;i++)
{
*(u+i) = *(t+i);
}
delete t;
}
{
if(size==1)
{
return;
}
//divide the array into 2
//sort each
merge_sort(u, size/2);
merge_sort(u+(size/2), size-(size/2));
int* t=(int*)malloc(sizeof(int)*(size));
for(int i=0,anext=0,bnext=0; i< size; i++)
{
if(anext==size/2)
{
t[i] = *(u+(size/2)+bnext);
bnext++;
}
else if(bnext == size-(size/2))
{
t[i] = *(u+anext);
anext++;
}
else
{
if(*(u+anext) < *(u+(size/2)+bnext))
{
t[i] = *(u+anext);
anext++;
}
else
{
t[i] = *(u+(size/2)+bnext);
bnext++;
}
}
}
//copy back to org array
for(int i=0;i< size;i++)
{
*(u+i) = *(t+i);
}
delete t;
}
find kth smallest element in an array
void kthsmallest(int k, int l, int u)
{
if(l>= u)
return;
//partition
int m=l;
for(int i=l+1; i<= u; i++)
{
if(arr[i] < arr[l])
{
//swap them
int t=arr[i];
arr[i]=arr[m+1];
arr[m+1]=t;
m++;
}
for(int j=0;j< 8;j++)
cout << " " << arr[j];
cout << " m= " << m << " i= " << i << endl;
}
//swap l, m
int t=arr[m];
arr[m]=arr[l];
arr[l]=t;
for(int i=0;i< 8;i++)
cout << " " << arr[i];
cout << " [" << arr[m] << "] is now correctly placed at position " << m ;
cout << endl;
/*
if(m==k)
cout << k << "th smallest element is " << arr[m] << endl;
*/
//recursively call qsort
if(k< m)
kthsmallest(k, l, m-1);
if(k> m)
kthsmallest(k, m+1, u);
}
//program end
{
if(l>= u)
return;
//partition
int m=l;
for(int i=l+1; i<= u; i++)
{
if(arr[i] < arr[l])
{
//swap them
int t=arr[i];
arr[i]=arr[m+1];
arr[m+1]=t;
m++;
}
for(int j=0;j< 8;j++)
cout << " " << arr[j];
cout << " m= " << m << " i= " << i << endl;
}
//swap l, m
int t=arr[m];
arr[m]=arr[l];
arr[l]=t;
for(int i=0;i< 8;i++)
cout << " " << arr[i];
cout << " [" << arr[m] << "] is now correctly placed at position " << m ;
cout << endl;
/*
if(m==k)
cout << k << "th smallest element is " << arr[m] << endl;
*/
//recursively call qsort
if(k< m)
kthsmallest(k, l, m-1);
if(k> m)
kthsmallest(k, m+1, u);
}
//program end
Labels:
algorithm,
array,
C,
C++,
data structure,
kth smallest
sort an array using quicksort
#include <iostream> using namespace std; void mquicksort(int A[], int l, int u) { if(l>=u) return; int m=l; for(int i=l+1; i<u; i++) { if(A[i] < A[l]) { int t=A[i]; A[i] = A[m+1]; A[m+1]=t; m++; } } int t=A[l]; A[l] = A[m]; A[m]=t; mquicksort(A, l, m-1); mquicksort(A, m+1, u); } int main() { // your code goes here int A[20]; int n; cout << "enter number of elements(<=20) to be sorted:"; cin >> n; cout << "\nenter " << n << " elements now\n"; for(int i=0;i<n;i++) { cin >> A[i]; } mquicksort(A, 0, n); cout << "\n\nsorted numbers: "; for(int i=0;i<n;i++) { cout << A[i] << " "; } return 0; }
Generate k unique random numbers ranging from 1 to n
void genrandomnum(int k, int n)
{
cout << "algo1: ";
int arr[100];
for(int i=0;i < 100;i++)
arr[i] = 0;
int i=0;
while(i!=k)
{
int rnd = (rand()%n) + 1; //random num in 1,n
if(arr[rnd] == 0)
{
arr[rnd] = -1;
arr[i] = rnd;
i++;
}
else
{
if(arr[rnd] > 0)
{
arr[rnd] = -arr[rnd]; //mark
arr[i] = rnd;
i++;
}
}
}
for(int i=0;i < k;i++)
if(arr[i]<0)
arr[i]=-arr[i];
for(int i=0;i < k;i++)
cout << " " << arr[i];
cout << endl << endl;
}
//Knuth's Algo
void genrandomnum1(int k, int n)
{
cout << "algo2: " ;
for(int i=0; i < n; i++)
{
if((rand()%(n-i)) < k)
{
cout << i << " ";
k--;
}
}
cout << endl << endl;
}
{
cout << "algo1: ";
int arr[100];
for(int i=0;i < 100;i++)
arr[i] = 0;
int i=0;
while(i!=k)
{
int rnd = (rand()%n) + 1; //random num in 1,n
if(arr[rnd] == 0)
{
arr[rnd] = -1;
arr[i] = rnd;
i++;
}
else
{
if(arr[rnd] > 0)
{
arr[rnd] = -arr[rnd]; //mark
arr[i] = rnd;
i++;
}
}
}
for(int i=0;i < k;i++)
if(arr[i]<0)
arr[i]=-arr[i];
for(int i=0;i < k;i++)
cout << " " << arr[i];
cout << endl << endl;
}
//Knuth's Algo
void genrandomnum1(int k, int n)
{
cout << "algo2: " ;
for(int i=0; i < n; i++)
{
if((rand()%(n-i)) < k)
{
cout << i << " ";
k--;
}
}
cout << endl << endl;
}
Labels:
algorithm,
array,
C,
C++,
data structure,
Knuth,
random numbers
Subscribe to:
Posts (Atom)