0% found this document useful (0 votes)
45 views

Data Structure: Name:Naveen Kumar

The document describes several sorting and searching algorithms: 1) Insertion sort works by building a sorted array from individual items, similar to arranging cards. 2) Selection sort finds the minimum element and places it at the start of the array repeatedly. 3) Binary search divides the search space in half at each step to locate an element in a sorted array faster than linear search.

Uploaded by

Naveen Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Data Structure: Name:Naveen Kumar

The document describes several sorting and searching algorithms: 1) Insertion sort works by building a sorted array from individual items, similar to arranging cards. 2) Selection sort finds the minimum element and places it at the start of the array repeatedly. 3) Binary search divides the search space in half at each step to locate an element in a sorted array faster than linear search.

Uploaded by

Naveen Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA STRUCTURE

NAME:NAVEEN KUMAR

ROLL-NO:2K18/CO/224
AIM:perform insertion sort

THEORY:insertion sort is the sorting mechanism where the sorted array is built having one item at a
time. The analogy can be understood from the style we arrange a deck of cards. This sort works on the
principle of inserting an element at a particular position to make it sorted.

Algorithm:

 Initialize all variables


 Start from index 1 and start comparing with its adjacent element.
 If the element is greater than adjacent element ,stop and insert the value.
 Repeat the step 3 untill n elements are couverd.

CODE:

#include<iostream>
using namespace std;

Void swap(int*a,int*b)
{
Int temp=*a;
*a=*b;
*b=temp;
}
Int main()
{
Int n;
Int ar[n+2];
Int j=0;
For(int i=0;i<n;i++)
{
J=I;
While(ar[j]<ar[j-1]&&j>=0)
{
Swap(&ar[j],&ar[j-1]);
j--;
}
}
Return 0;
}
Result: the output of the program comes as expected hence sorting of array is correctly performed using
insertion sort;
Conlusion: throughout this program I have learnt about how to sort an array using insertion sort
algorithm and also learnt about its space and time complexity.
Aim:perform selection sort on an array
Theory: the selection sort algorithm sorts an array by repeatedly finding the minimum element from
unsorted part and putting it at the beginning . Time complexity is O(N^2)
Algorithm:
 Declare and initialize variables
 Find the minimum from array and swap with starting element(i) element
 The find the second minimum element from idex 1 to n-1
 Repeat step 2 and 3 untill the array is sorted
CODE:
#inlcude<iostream>
Using namespace std;
Void swap()
{
Int temp =*a;
*a=*b;
*b=temp;
}
Int main()
{
Int n;
Cin>>n;
Int ar[n+2];
For(int i=0;i<n;i++)
Cin>>ar[i];
Int min;
For(int i=0;i<n-1;i+++)
{
Min=I;
For(int j=i+1;j<n;j++)
{
If(ar[min]>ar[j])
Min=j;
}
Swap(&ar[min],&ar[i]);
}
Return 0;
}
Result: the output of the program is as expected hence selection sort is performed correctly
Conclusion: In this ,program, I have learnt about how to sort elements of array using selection sort
algorithm
Aim: perform binary search on sorted array
Theory:
Binary search is a algorithm which is used to search an element in sorted array in O(logn) time
complexity, using divide and conquer rule, where n is number of elements in an array.
Algorithm:
 Declare and initialize variables
 Take inputs and sort the array
 Now compare the array the given element with middle element of array,If it equal print
“element found”.
 If element is less than middle element then search in left part of middle
 If element is greater than middle element then search in right part of middle
 This process continues until we found the element ,eles print -1 or not found
Code:
#include<iostream>
Using namespace std;
Void swap(int*a,int*b)
{
Int temp=*a;
*a=*b;
*b=temp;
}
Void sort_array(in tar[],int n)
{
Int j;
For(int i=1;i<n;i++)
{
J=I;
While(j>0&&ar[j-1]>ar[j])
{
Swap(&ar[j-1],&ar[j]);
j--;
}
}
}
Void binary_search(in tar[],int n,int x)
{
Int mid=n/2;
Int l=0,r=n;
While(l<=r)
{
If(ar[mid]==x)
{
Cout<<”element found”;
Return;
}
Else if(ar[mid]<n)
r=mid+1;
Else
r=mid-1;
mid=(l+r)/2;
}
Cout<<”element not found”;
}
Int main()
{
Int n,x;
Int r[n+2];
Cout<<”enter the no. of element in the array”<<endl;
Cin>>n;
For(int i=0;i<n;i++)
Cin>>ar[i];
Cout<<” enter the element to be searched”<<endl;
Cin>>x;
Sort_array(ar,n);
Binary_search(ar,n,x);
Return 0;
}
Result: binary search is perfectly working on all the cases
Conclusion: in this program , I have learnt about how to apply divide and conquer rule to find element in
a sorted array.

You might also like