04 Program

  • Uploaded by: Class 12 A
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 04 Program as PDF for free.

More details

  • Words: 174
  • Pages: 2
Program #include #include void insertionsort(int array[], int size) //Insertion Sort Function { int temp, i, j; for (i=0;i<size;i++) { temp = array[i]; //Pick the first element of unsorted array j = i-1; while(temp<array[j] && j>=0) //Scan for its proper place { array[j+1] = array[j]; j = j - 1; } array[j+1] = temp; //Insert the element at its proper place } } int binarysearch(int arr[], int size, int query) { int first, mid, last; first=0; last=size-1; while(first<=last) { mid=(first+last)/2; if (query = = arr[mid]) return mid; else if (query>arr[mid]) first=mid+1; else last=mid-1; } return -1; }

//Binary Search Function

void main() { clrscr(); int arr[50], item, n, index, query, res; cout<<"Enter the number of elements: "; cin>>n; cout<<"Enter the elements: "; for (int i=0; i>arr[i]; } insertionsort(arr, n); cout<<"\n\nThe sorted array is:\n"; for (i=0; i
//Calling insertoinsort()

{ }

cout<<arr[i]<<"\t";

cout<<"\n\nEnter the element to be searched: "; cin>>query; res=binarysearch(arr, n, query);

}

//Calling binarysearch()

if (res = = -1) cout<<"Element not found "; else cout<<"Element found at position "<

Related Documents

04 Program
November 2019 19
04. Program Linen.docx
December 2019 13
Exchange Program 04
April 2020 7
Program
May 2020 25
Program
October 2019 41

More Documents from ""

08 Output
November 2019 18
02 Program
November 2019 20
05 Program
November 2019 14
01 Program
November 2019 15
04 Program
November 2019 19
03 Program
November 2019 8