0% found this document useful (0 votes)
16 views14 pages

Manish

The document contains programming assignments for a B.Tech III Year CSE course on Design and Analysis of Algorithms. It includes implementations of various search and sorting algorithms such as Linear Search, Binary Search, Bubble Sort, Quick Sort, Merge Sort, and Insertion Sort. Each program is written in C++ and includes input/output instructions along with the code.

Uploaded by

dvraj1717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Manish

The document contains programming assignments for a B.Tech III Year CSE course on Design and Analysis of Algorithms. It includes implementations of various search and sorting algorithms such as Linear Search, Binary Search, Bubble Sort, Quick Sort, Merge Sort, and Insertion Sort. Each program is written in C++ and includes input/output instructions along with the code.

Uploaded by

dvraj1717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

S.No. Program Date Sign


COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

Program – 1

Q. Write a program to implement Linear Search.


#include <iostream>
using namespace std;
int linearSearch(int arr[], int key, int
size){ for (int i = 0; i < size; i++){
if (key == arr[i]) return i;
} return -1; } int main()
{ int size, key; cout << "Enter
size of array: "; cin >> size;
int arr[size];
cout << "Enter elements of
array: "; for (int i = 0; i <
size; i++) cin >> arr[i];
cout << "Enter element to be searched:
"; cin >> key;
int ans = binarySearch(arr, key, size);
if (ans != -1) cout << "Element found at
index: " << ans;
else cout << "Element not
found"; return 0;}
Name :- Deepak Agrawal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

OUTPUT:
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

Program – 2

Q. Write a program to implement Binary Search.


#include <iostream>
using namespace std;
int binarySearch(int arr[], int key, int
size){ int s = 0, e = size - 1; while
(s <= e){ int mid = s + (e - s) / 2; if
(arr[mid] == key) return mid;
else if (arr[mid] >
key) e = mid - 1;
else

s = mid + 1;
} return -1; } int main(){ int size,
key; cout << "Enter size of
array: "; cin >> size; int
arr[size];
cout << "Enter elements of
array: "; for (int i = 0; i <
size; i++){ cin >> arr[i];
}
cout << "Enter element to be searched:
"; cin >> key;
Name :- Deepak Agrawal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI


int ans = binarySearch(arr, key, size); if (ans !=
-1) cout << "Element found at index: " << ans;
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

else cout << "Element not


found";
return 0;
}

OUTPUT:
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

Program – 3

Q. Write a program to implement Bubble Sort.


#include <iostream> #include
<algorithm> using namespace std;
void printArray(int arr[], int
size){ for (int i = 0; i < size;
i++) cout << arr[i] << ' ';
}
void bubbleSort(int arr[], int size)
{ for (int i = 0; i < size - 1; i+
+){ for (int j = 0; j < size - 1;
j++){ if (arr[j] > arr[j + 1])
swap(arr[j + 1], arr[j]);
}
} } int main(){ int size,
key; cout << "Enter size of
array: "; cin >> size; int
arr[size];
cout << "Enter elements of
array: "; for (int i = 0; i <
size; i++) cin >> arr[i];
cout << "The array is: " <<
endl; printArray(arr, size);
bubbleSort(arr, size);
Name :- Deepak Agrawal Class :- B.Tech III Year, CSE
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI


cout << endl << "Sorted array is: " << endl;
printArray(arr, size); return 0;
}

OUTPUT:
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

Program – 4

Q. Write a program to implement Quick Sort.


#include <iostream> using
namespace std; void
printArray(int arr[], int size)
{ for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
}
int partition(int arr[], int start, int end)
{ int pivot = arr[start], i = start + 1, j
= end; while (i < j){ while (arr[i] <=
pivot && i < end) i++; while (arr[j] >
pivot && j > start) j--; if (i < j)
swap(arr[i], arr[j]);
}
swap(arr[start],
arr[j]); return j; }
void quickSort(int arr[], int start, int
end){ if (start < end){ int p =
partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
} } int main(){ int size,
key; cout << "Enter size of
array: ";
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

cin >> size;


int arr[size];
cout << "Enter elements of
array: "; for (int i = 0; i <
size; i++) cin >> arr[i];
cout << "The array is: " << endl;
printArray(arr, size); quickSort(arr,
0, size - 1); cout << endl << "Sorted
array is: " << endl; printArray(arr,
size); return 0;
}

OUTPUT:

Program – 5

Q. Write a program to implement Merge Sort.


#include <iostream>
using namespace std;
void printArray(int arr[], int
size){ for (int i = 0; i <
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

size; i++) cout << arr[i] << '


';
}
void merge(int arr[], int beg, int mid, int
end){ int i, j, k; int n1 = mid - beg +
1; int n2 = end - mid;
int LeftArray[n1], RightArray[n2]; // temporary arrays
/* copy data to temp arrays
*/ for (int i = 0; i < n1; i+
+)
LeftArray[i] = arr[beg +
i]; for (int j = 0; j < n2;
j++)
RightArray[j] = arr[mid + 1 + j]; i =
0; /* initial index of first sub-array */ j
= 0; /* initial index of second sub-array
*/ k = beg; /* initial index of merged sub-
array */ while (i < n1 && j < n2){ if
(LeftArray[i] <= RightArray[j]){ arr[k] =
LeftArray[i]; i++;
} else { arr[k] =
RightArray[j]; j++;
} k++; } while (i <
n1){ arr[k] =
LeftArray[i]; i++; k+
+; } while (j < n2)
{ arr[k] =
RightArray[j]; j++; k++;
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR
Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

}
}
void mergeSort(int arr[], int beg, int
end){ if (beg < end) { int mid =
(beg + end) / 2; mergeSort(arr, beg,
mid); mergeSort(arr, mid + 1, end);
merge(arr, beg, mid, end);
} } int main(){ int size, key;
cout << "Enter size of array: "; cin
>> size; int arr[size]; cout <<
"Enter elements of array: "; for
(int i = 0; i < size; i++) cin >>
arr[i]; cout << "The array is: " <<
endl;
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI


printArray(arr, size); mergeSort(arr, 0, size - 1);
cout << endl << "Sorted array is: " <<
endl; printArray(arr, size); return 0;
}

OUTPUT:
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

Name :- Manish Rajwal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI

Program – 6

Q. Write a program to implement Insertion Sort.


#include
<iostream>
#include
<algorithm> using
namespace std;
void printArray(int arr[], int
size){ for (int i = 0; i <
size; i++) cout << arr[i] <<
' ';
}
void insertionSort(int arr[], int
size){ for (int i = 1; i < size;
i++){ int currEle = arr[i]; int
j = i - 1;
while (arr[j] > currEle && j >=
0){ arr[j + 1] = arr[j]; j--; }
arr[j + 1] = currEle;
} } int
main(){ int
size, key;
cout << "Enter size of
array: "; cin >> size; int
arr[size];
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR

cout << "Enter elements of


array: "; for (int i = 0; i <
size; i++) cin >> arr[i];
Name :- Deepak Agrawal Class :- B.Tech III Year, CSE

Sub:- Design and Analysis of Algorithms (CS- 362) Sem :- VI


cout << "The array is: " << endl; printArray(arr,
size); mergeSort(arr, 0, size - 1);
cout << endl << "Sorted array is: " <<
endl; printArray(arr, size); return 0;
}

OUTPUT:

You might also like