1. Write a function to implement insertion sort
void insertion_sort(int *arr,int sz) { int i,j,k; for(i = 1;i<sz;i++) { int temp = arr[i]; int j = i-1; while(j>=0 && arr[j]>temp) { arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } }
2. Explain shell sort with its implementation
#include<stdio.h> void shell_sort(int *arr,int n) { int gap,i; for(gap=n/2;gap>0;gap/=2) { int i; for(i=gap;i<n;i++) { int temp = arr[i]; int j; for(j=i;j>=gap&&arr[j-gap]>temp;j-=gap) arr[j] = arr[j-gap]; arr[j]=temp; } } } void read_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); scanf("%d",&arr[i]); } } void print_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); printf("%d ",arr[i]); } printf("\n"); } int main() { int arr[50]; int sz; printf("Enter the size of array:"); scanf("%d",&sz); read_array(arr,sz); shell_sort(arr,sz); printf("The sorted array is "); print_array(arr,sz); return 0; }
3. Explain the quick sort algorithm. Write a function for quick sort
void quicksort(int *arr,int lo,int hi) { if (lo < hi) { int p =partition(arr, lo, hi); quicksort(arr,lo,p-1); quicksort(arr, p + 1, hi); } } void swap(int *ptr1,int *ptr2) { int temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } int partition(int *arr,int low,int high) { int i,j,pivot; i = low+1; pivot = arr[low]; for(j=low+1;j<=high;j++) if(arr[j]<pivot) { swap(&arr[j],&arr[i]); i++; } swap(&arr[low],&arr[i-1]); return i-1; }
4. How efficient is sequential search? Explain different methods used to get maximum search efficiency.
int sequential_search(int *arr,int sz,int sv) { int i; for(i=0;i<sz &&arr[i]!=sv;i++) ; if(arr[i]==sv) return i; return -1; } int sequential_search2(int *arr,int sz,int sv) { int i; arr[sz]=sv;/*save a sentinel value*/ for(i=0; arr[i]!=sv;i++) ; if(i<sz) return i; return -1; } int sequential_search3(int *arr,int sz,int sv) { /*the array is already sorted stop searching if value is greater than n*/ int i; for(i=0;i<sz && arr[i]<sv;i++) ; if(arr[i]==sv) return i; return -1; }
5. Write an algorithm and function for selection sorting.
void selection_sort(int *arr,int sz) { int i,j,k; for(i = 0;i<sz-1;i++) { int s = i; for(j=i+1;j<sz-1;j++) if(arr[j]<arr[s]) s = j; if(arr[s]<arr[i])/*ith smallest*/ { int temp = arr[s]; arr[s] = arr[i]; arr[i] = temp; } } }
6. Explain interpolation search
#include<stdio.h> int interpolation_search(int arr[], int size, int key) { int low = 0; int high = size - 1; int mid; while ((arr[high] != arr[low]) && (key >= arr[low]) && (key <= arr[high])) { mid = low + ((key - arr[low]) * (high - low) / (arr[high] - arr[low])); if (arr[mid] < key) low = mid + 1; else if (key < arr[mid]) high = mid - 1; else return mid; } if (key == arr[low]) return low ; else return -1; } void print_array(int *arr,int sz) { int i; printf("Elements of the array are:"); for(i =0;i<sz;i++) { printf("arr[%d]=",i); printf("%d ",arr[i]); } printf("\n"); } int main() { int n; int arr[] = {11,33,55,88,211,899}; print_array(arr,6); while(1) { printf("Enter the value to search (-1000 to exit):"); scanf("%d",&n); if(n==-1000) break; n = interpolation_search(arr,6,n); if(n!=-1) printf("The array element is found at %d\n",n); else printf("Not found\n"); } }
7. Write a function to search an array using binary search
#include<stdio.h> void read_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); scanf("%d",&arr[i]); } } void print_array(int *arr,int sz) { int i; printf("Enter the elements of sorted array:"); for(i =0;i<sz;i++) { printf("arr[%d]=",i); printf("%d ",arr[i]); } printf("\n"); } int binary_search(int *arr,int start,int end,int val) { if(start<=end){ int mid = (start+end)/2; if(arr[mid]==val) return mid; else if(arr[mid]>val) return binary_search(arr,start,mid-1,val); else return binary_search(arr,mid+1,end,val); } else return -1; } int main() { int arr[50]; int sz; printf("Enter the size of array:"); scanf("%d",&sz); read_array(arr,sz); while(1) { int n,index; printf("Enter the value to search, -1 to exit"); scanf("%d",&n); if(n==-1) break; if((index=binary_search(arr,0,sz-1,n))!=-1) printf("Value found at index %d\n",index); else printf("Value not found\n"); } return 0; }
8. Write a function to merge sort an array.
#include<stdio.h> void merge_sorted_arrays(int *arr,int st1,int md1,int end1); void merge_sort(int *arr,int start,int end) { if(start<end){ int mid = (start+end)/2; merge_sort(arr,start,mid); merge_sort(arr,mid+1,end); merge_sorted_arrays(arr,start,mid,end); } } void merge_sorted_arrays(int *arr ,int st1,int md1,int end1) { int i,j,k;int temp[50]; i = j = k=0; i = st1; j=md1+1; while(i<=md1 && j<=end1) { if(arr[i]<arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while(i<=md1) temp[k++] = arr[i++]; while(j<=end1) temp[k++] = arr[j++]; for(i=st1,k=0;i<=md1;i++,k++) arr[i]=temp[k]; for(j=md1+1 ;j<=end1;j++,k++) arr[j]=temp[k]; } void read_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); scanf("%d",&arr[i]); } } void print_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); printf("%d ",arr[i]); } printf("\n"); } int main() { int arr1[50] ; int sz ; printf("Enter the size of array:"); scanf("%d",&sz); printf("Enter the array elements:"); read_array(arr1,sz); merge_sort(arr1,0,sz-1); printf("The sorted array is "); print_array(arr1,sz); return 0; }
9. Write a program to heapsort an array.
#include <stdio.h> void printArray(int *arr,int n); void swap(int *a,int *b) { int temp = *a; *a = *b; *b = temp; } /* convert node at index i into a max heap */ void heapify(int arr[], int n, int i) { int largest = i; int left = 2*i + 1; // left = 2*i + 1 int right = 2*i + 2; // right = 2*i + 2 if (left < n && arr[left] > arr[largest]) largest = left; if (right < n && arr[right] > arr[largest]) largest = right; if (largest != i) { swap(&arr[i], &arr[largest]); heapify(arr, n, largest);/*now node at largest is out of order. heapify it*/ } } void heapSort(int arr[], int n) { int i; /* Build heap from array elements*/ for ( i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); /*Extract 0th element (root) and move it at the end of array. Reduce the size of heap*/ for ( i=n-1; i>=0; i--) { /* Move current root to end*/ swap(&arr[0], &arr[i]); /* call max heapify on the reduced heap*/ heapify(arr, i, 0); } } void readArray(int arr[], int n) { int i; for ( i=0; i<n; ++i) { printf("a[%d]",i); scanf("%d",&arr[i]); } } void printArray(int arr[], int n) { int i; for ( i=0; i<n; ++i) printf("%d ",arr[i]); printf( "\n"); } // Driver program int main() { int arr[40];int n; printf("What is array size"); scanf("%d",&n); readArray(arr,n); heapSort(arr, n); printf("Sorted array is \n"); printArray(arr, n); }
10. Write a program to search a value in an array using linear searching method.
#include<stdio.h> void read_array(int *arr,int sz) { int i; for(i =0;i<sz;i++) { printf("arr[%d]=",i); scanf("%d",&arr[i]); } } void print_array(int *arr,int sz) { int i; printf("Enter the elements of sorted array:"); for(i =0;i<sz;i++) { printf("arr[%d]=",i); printf("%d ",arr[i]); } printf("\n"); } int linear_search(int *arr,int n,int val) { int index = -1; int i; for(i=0;i<n;i++) if(arr[i]==val) return i; return index; } int main() { int arr[50]; int sz; printf("Enter the size of array:"); scanf("%d",&sz); read_array(arr,sz); while(1) { int n,index,val; printf("Enter the value to search, -1 to exit"); scanf("%d",&val); if(val==-1) break; if((index=linear_search(arr, sz,val))!=-1) printf("Value found at index %d\n",index); else printf("Value not found\n"); } return 0; }
11. What are the different methods of search algorithms? Explain.