You are on page 1of 13

Linear search

Q.1. #include <iostream>

using namespace std;

class LS

public:

void LinearSearch(int arr[], int value, int i, int n)

{ int found = 0;

for (i = 0; i < n ; i++)

if (value == arr[i] )

found = 1;

break;

if (found == 1)

cout<<"Element is present in the array at position "<<i+1;

else

cout<<"Element is not present in the array.";

}
};

int main()

{ int num;

int i, keynum, found = 0;

cout<<"Enter the number of elements ";

cin>>num;

int array[num];

cout<<"Enter the elements one by one \n";

for (i = 0; i < num; i++)

cin>> array[i];

cout<<"Enter the element to be searched ";

cin>>keynum;

/* Linear search begins */

LS l1;

l1.LinearSearch(array,keynum,i,num);

return 0;
}

Q.2.multipication of matrix

#include <iostream>

using namespace std;

int main(){

int matrix1[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int matrix2[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int results[3][3];

int product = 0;

int i;

int j;

for (i = 1; i <= 3; i++){


for (j = 1; j <= 3; j++){

product += matrix1[i][j] * matrix2[j][i];

cout << product << endl;

results[i][j] = product;

product = 0;

cout << endl << "Output Matrix: " << endl;

for (int i = 1; i < 4; i++){

for (int j = 1; j < 4; j++){

cout << results[i][j];

cout << endl;

system("pause");

return 0;

output
q.3. insertion sort

#include<iostream>

using namespace std;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;

void insertionSort(int *array, int size) {

int key, j;

for(int i = 1; i<size; i++) {

key = array[i];//take value

j = i;

while(j > 0 && array[j-1]>key) {

array[j] = array[j-1];
j--;

array[j] = key; //insert in right place

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n]; //create an array with given number of elements

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

insertionSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

output
q.4. selection sort

#include<iostream>

using namespace std;

void swapping(int &a, int &b) { //swap the content of a and b

int temp;

temp = a;

a = b;

b = temp;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;

void selectionSort(int *array, int size) {

int i, j, imin;

for(i = 0; i<size-1; i++) {

imin = i; //get index of minimum data


for(j = i+1; j<size; j++)

if(array[j] < array[imin])

imin = j;

//placing in correct position

swap(array[i], array[imin]);

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n]; //create an array with given number of elements

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

selectionSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

output
Q.5. buble sort

#include<iostream>

using namespace std;

void swapping(int &a, int &b) { //swap the content of a and b

int temp;

temp = a;

a = b;

b = temp;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;

void bubbleSort(int *array, int size) {

for(int i = 0; i<size; i++) {

int swaps = 0; //flag to detect any swap is there or not

for(int j = 0; j<size-i-1; j++) {


if(array[j] > array[j+1]) { //when the current item is bigger
than next

swapping(array[j], array[j+1]);

swaps = 1; //set swap flag

if(!swaps)

break; // No swap in this pass, so array is sorted

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n]; //create an array with given number of elements

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

bubbleSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

output
q.6. binary search

#include<iostream>

using namespace std;

int main()

int search(int [],int,int);

int n,i,a[100],e,res;

cout<<"How Many Elements:";

cin>>n;

cout<<"\nEnter Elements of Array in Ascending order\n";

for(i=0;i<n;++i)

cin>>a[i];

}
cout<<"\nEnter element to search:";

cin>>e;

res=search(a,n,e);

if(res!=-1)

cout<<"\nElement found at position "<<res+1;

else

cout<<"\nElement is not found....!!!";

return 0;

int search(int a[],int n,int e)

int f,l,m;

f=0;

l=n-1;

while(f<=l)

m=(f+l)/2;

if(e==a[m])

return(m);

else

if(e>a[m])
f=m+1;

else

l=m-1;

return -1;

Output;

You might also like