SlideShare a Scribd company logo
Code
1. Binary Search:
#include <iostream>
using namespace std;
int binary_search(int a[], int n, int data) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a[mid] == data)
return mid;
if (a[mid] < data)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int n;
cout << "Enter Array Size: ";
cin >> n;
int a[n];
cout << "Enter Array Elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int data;
cout << "Enter Data You Want To Search: ";
cin >> data;
int result = binary_search(a, n, data);
if (result == -1)
cout << "Not found" << endl;
else
cout << "Found" << endl;
return 0;
}
Output :
Explanation :
we look for a particular number in a sorted list by repeatedly dividing the list in half. First, we
check the middle number and compare it with the number we want to find. If they are the
same, the search is successful. If not, we adjust the range of numbers we're looking at (lower or
higher) and continue the process until we find the desired number.
2. Binary Search with Recursion:
#include <iostream>
using namespace std;
int binary_search(int a[], int x, int low, int high) {
while (high >= low) {
int mid = low + (high - low) / 2;
if (a[mid] == x)
return 1;
if (a[mid] > x)
return binary_search(a, x, low, mid - 1);
else
return binary_search(a, x, mid + 1, high);
}
return -1;
}
int main() {
int n;
cout << "Enter Array Size: ";
cin >> n;
int a[n];
cout << "Enter Array Elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int data;
cout << "Enter Data You Want To Search: ";
cin >> data;
int result = binary_search(a, data, 0, n - 1);
if (result == -1)
cout << "Not Found" << endl;
else
cout << "Found" << endl;
return 0;
}
Output :
Explanation:
Binary search with recursion helps find a number in a sorted list. It works by dividing the list in
half and checking the middle element. If the middle element is the same as the number we
want to find, we found it! If the middle element is smaller than the number we want, we repeat
the same process with the lower half of the list. But if the middle element is larger, we do the
process with the upper half of the list. We keep doing this until we find the number we're
looking for or if it's not in the list.
3. Linear Search:
#include<iostream>
using namespace std;
void linear_search(int a[], int n, int num) {
for (int i = 0; i < n; i++) {
if (a[i] == num) {
cout << "Found & Index The Number: " << i << endl;
return;
}
}
cout << "Not Found" << endl;
}
int main() {
int n;
cout << "Array Size: ";
cin >> n;
int a[n];
cout << "Array: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int num;
cout << "You Want to Search The Number : ";
cin >> num;
linear_search(a, n, num);
return 0;
}
Output :
Explanation:
In this process, we go through the entire array that we get from the users. Then, we compare
each element of the array with the number we want to find. If we find a match, we return the
index of that number and display it. After that, we exit the loop. If we don't find the number in
the whole array, we print "Not found".
4. Insertion Sort :
#include <iostream>
using namespace std;
void insertion_sort(int a[], int n) {
for (int i = 1; i < n; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
}
int main() {
int n;
cout << "Array Size: ";
cin >> n;
int a[n];
cout << "Array: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
insertion_sort(a, n);
cout << "Sorted Array:" << endl;
for (int i = 0; i < n; i++) {
cout << a[i] << endl;
}
return 0;
}
Output :
Explanation :
This sorting method divides a list into two parts: the sorted part and the unsorted part. It takes
elements from the unsorted part one by one and places them into their appropriate positions
within the sorted part. To achieve this, it compares the current element with the elements in
the sorted part, moving larger elements to the right until it finds the correct position for the
current element. This process continues until all elements are correctly positioned in the sorted
part, resulting in a fully sorted list.
5. Selection Sort:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Array Size: ";
cin >> n;
int a[n];
cout << "Array: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
int min_pos = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[min_pos]) {
min_pos = j;
}
}
if (min_pos != i) {
int temp = a[i];
a[i] = a[min_pos];
a[min_pos] = temp;
}
}
cout << "Selection Sort:" << endl;
for (int i = 0; i < n; i++) {
cout << a[i] << endl;
}
return 0;
}
Output:
Explanation:
We use a sorting algorithm called selection sort to sort the list. It works by dividing the list into
two parts: the sorted part and the unsorted part. In each iteration, it finds the smallest element
in the unsorted part and swaps it with the first element of the unsorted part. This process is
repeated until the entire list is sorted, and there is no unsorted part left. As a result, the list
becomes completely sorted in ascending order.
6. Merge Sort:
#include <iostream>
using namespace std;
void merge_sort(int a[], int length);
void merge_sort_recursion(int a[], int l, int r);
void merge_sorted_arrays(int a[], int l, int m, int r);
int main() {
int length;
cout << "Array Size: ";
cin >> length;
int array[length];
cout << "Array: ";
for (int i = 0; i < length; i++) {
cin >> array[i];
}
merge_sort(array, length);
cout << "Merge Sort:" << endl;
for (int i = 0; i < length; i++) {
cout << array[i] << endl;
}
return 0;
}
void merge_sort(int a[], int length) {
merge_sort_recursion(a, 0, length - 1);
}
void merge_sort_recursion(int a[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
merge_sort_recursion(a, l, m);
merge_sort_recursion(a, m + 1, r);
merge_sorted_arrays(a, l, m, r);
}
}
void merge_sorted_arrays(int a[], int l, int m, int r) {
int left_length = m - l + 1;
int right_length = r - m;
int temp_left[left_length];
int temp_right[right_length];
for (int i = 0; i < left_length; i++)
temp_left[i] = a[l + i];
for (int i = 0; i < right_length; i++)
temp_right[i] = a[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < left_length && j < right_length) {
if (temp_left[i] <= temp_right[j]) {
a[k] = temp_left[i];
i++;
} else {
a[k] = temp_right[j];
j++;
}
k++;
}
while (i < left_length) {
a[k] = temp_left[i];
i++;
k++;
}
while (j < right_length) {
a[k] = temp_right[j];
j++;
k++;
}
}
Output:
Explanation:
Here splits the list into small pieces, each with just one or no elements. It then puts these
pieces back together in a sorted order. It keeps doing this until the whole list is sorted. So, first
splits the whole list then it's sorted again
7. Counting Inversion:
#include <iostream>
using namespace std;
int inversion_count(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
count++;
}
}
return count;
}
int main()
{
int n;
cout << "Array Size:";
cin >> n;
int arr[n];
cout << "Array:";
for(int i=0; i<n; i++)
{
cin >> arr[i];
}
cout << " Inversions Count:" << inversion_count(arr, n);
return 0;
}
Output:
Explanation:
In the sorting process, we keep track of how many times a smaller number appears after a
larger number, known as an "inversion." After completing the sorting, we calculate and provide
the total count of inversions present in the array. This count helps us understand the
complexity and order of elements in the sorted array.

More Related Content

DOC
Ds program-print
PDF
Recursion Lecture in C++
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
ch16.pptx
PPTX
ch16 (1).pptx
PPTX
Recursion
Ds program-print
Recursion Lecture in C++
C++ Searching & Sorting5. Sort the following list using the select.pdf
Chapter 2 Sorting and Searching .pptx.soft
Chapter 2 Sorting and Searching .pptx.soft
ch16.pptx
ch16 (1).pptx
Recursion

Similar to Code (20)

PDF
Chapter Two.pdf
PDF
a) Write the recursive function in C++ to sort a set of data using M.pdf
DOCX
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PDF
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
PDF
#include iostream using namespace std; void InsertionSort(int .pdf
PPT
search_sort.ppt
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PDF
Recursion Lecture in Java
PPTX
Merge radix-sort-algorithm
PPTX
Merge radix-sort-algorithm
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPTX
Programming Fundamentals lecture-14.pptx
PPTX
Data Structure and algorithms for software
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPTX
PPT.pptx Searching and Sorting Techniques
DOCX
Write a program to find the number of comparisons using the binary se.docx
PDF
I have written the code but cannot complete the assignment please help.pdf
PDF
Searching and sorting by B kirron Reddi
PDF
Sorting Algorithms and their implementations
DOCX
Assignment
Chapter Two.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdf
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
#include iostream using namespace std; void InsertionSort(int .pdf
search_sort.ppt
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Recursion Lecture in Java
Merge radix-sort-algorithm
Merge radix-sort-algorithm
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
Programming Fundamentals lecture-14.pptx
Data Structure and algorithms for software
search_sort Search sortSearch sortSearch sortSearch sort
PPT.pptx Searching and Sorting Techniques
Write a program to find the number of comparisons using the binary se.docx
I have written the code but cannot complete the assignment please help.pdf
Searching and sorting by B kirron Reddi
Sorting Algorithms and their implementations
Assignment
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
O5-L3 Freight Transport Ops (International) V1.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Yogi Goddess Pres Conference Studio Updates
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Orientation - ARALprogram of Deped to the Parents.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Ad

Code

  • 2. 1. Binary Search: #include <iostream> using namespace std; int binary_search(int a[], int n, int data) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; if (a[mid] == data) return mid; if (a[mid] < data) low = mid + 1; else high = mid - 1; } return -1; } int main() { int n; cout << "Enter Array Size: "; cin >> n; int a[n]; cout << "Enter Array Elements: ";
  • 3. for (int i = 0; i < n; i++) { cin >> a[i]; } int data; cout << "Enter Data You Want To Search: "; cin >> data; int result = binary_search(a, n, data); if (result == -1) cout << "Not found" << endl; else cout << "Found" << endl; return 0; } Output :
  • 4. Explanation : we look for a particular number in a sorted list by repeatedly dividing the list in half. First, we check the middle number and compare it with the number we want to find. If they are the same, the search is successful. If not, we adjust the range of numbers we're looking at (lower or higher) and continue the process until we find the desired number. 2. Binary Search with Recursion: #include <iostream> using namespace std; int binary_search(int a[], int x, int low, int high) { while (high >= low) { int mid = low + (high - low) / 2; if (a[mid] == x) return 1; if (a[mid] > x) return binary_search(a, x, low, mid - 1); else return binary_search(a, x, mid + 1, high); } return -1; } int main() { int n; cout << "Enter Array Size: "; cin >> n;
  • 5. int a[n]; cout << "Enter Array Elements: "; for (int i = 0; i < n; i++) { cin >> a[i]; } int data; cout << "Enter Data You Want To Search: "; cin >> data; int result = binary_search(a, data, 0, n - 1); if (result == -1) cout << "Not Found" << endl; else cout << "Found" << endl; return 0; } Output : Explanation: Binary search with recursion helps find a number in a sorted list. It works by dividing the list in half and checking the middle element. If the middle element is the same as the number we want to find, we found it! If the middle element is smaller than the number we want, we repeat the same process with the lower half of the list. But if the middle element is larger, we do the
  • 6. process with the upper half of the list. We keep doing this until we find the number we're looking for or if it's not in the list. 3. Linear Search: #include<iostream> using namespace std; void linear_search(int a[], int n, int num) { for (int i = 0; i < n; i++) { if (a[i] == num) { cout << "Found & Index The Number: " << i << endl; return; } } cout << "Not Found" << endl; } int main() { int n; cout << "Array Size: "; cin >> n; int a[n]; cout << "Array: "; for (int i = 0; i < n; i++) { cin >> a[i]; }
  • 7. int num; cout << "You Want to Search The Number : "; cin >> num; linear_search(a, n, num); return 0; } Output : Explanation: In this process, we go through the entire array that we get from the users. Then, we compare each element of the array with the number we want to find. If we find a match, we return the index of that number and display it. After that, we exit the loop. If we don't find the number in the whole array, we print "Not found". 4. Insertion Sort : #include <iostream> using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int key = a[i];
  • 8. int j = i - 1; while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j = j - 1; } a[j + 1] = key; } } int main() { int n; cout << "Array Size: "; cin >> n; int a[n]; cout << "Array: "; for (int i = 0; i < n; i++) { cin >> a[i]; } insertion_sort(a, n); cout << "Sorted Array:" << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; }
  • 9. return 0; } Output : Explanation : This sorting method divides a list into two parts: the sorted part and the unsorted part. It takes elements from the unsorted part one by one and places them into their appropriate positions within the sorted part. To achieve this, it compares the current element with the elements in the sorted part, moving larger elements to the right until it finds the correct position for the current element. This process continues until all elements are correctly positioned in the sorted part, resulting in a fully sorted list. 5. Selection Sort: #include <iostream> using namespace std; int main() { int n; cout << "Array Size: "; cin >> n;
  • 10. int a[n]; cout << "Array: "; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { int min_pos = i; for (int j = i + 1; j < n; j++) { if (a[j] < a[min_pos]) { min_pos = j; } } if (min_pos != i) { int temp = a[i]; a[i] = a[min_pos]; a[min_pos] = temp; } } cout << "Selection Sort:" << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; } Output:
  • 11. Explanation: We use a sorting algorithm called selection sort to sort the list. It works by dividing the list into two parts: the sorted part and the unsorted part. In each iteration, it finds the smallest element in the unsorted part and swaps it with the first element of the unsorted part. This process is repeated until the entire list is sorted, and there is no unsorted part left. As a result, the list becomes completely sorted in ascending order. 6. Merge Sort: #include <iostream> using namespace std; void merge_sort(int a[], int length); void merge_sort_recursion(int a[], int l, int r); void merge_sorted_arrays(int a[], int l, int m, int r); int main() { int length; cout << "Array Size: "; cin >> length;
  • 12. int array[length]; cout << "Array: "; for (int i = 0; i < length; i++) { cin >> array[i]; } merge_sort(array, length); cout << "Merge Sort:" << endl; for (int i = 0; i < length; i++) { cout << array[i] << endl; } return 0; } void merge_sort(int a[], int length) { merge_sort_recursion(a, 0, length - 1); } void merge_sort_recursion(int a[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; merge_sort_recursion(a, l, m); merge_sort_recursion(a, m + 1, r); merge_sorted_arrays(a, l, m, r); } }
  • 13. void merge_sorted_arrays(int a[], int l, int m, int r) { int left_length = m - l + 1; int right_length = r - m; int temp_left[left_length]; int temp_right[right_length]; for (int i = 0; i < left_length; i++) temp_left[i] = a[l + i]; for (int i = 0; i < right_length; i++) temp_right[i] = a[m + 1 + i]; int i = 0, j = 0, k = l; while (i < left_length && j < right_length) { if (temp_left[i] <= temp_right[j]) { a[k] = temp_left[i]; i++; } else { a[k] = temp_right[j]; j++; } k++; } while (i < left_length) { a[k] = temp_left[i]; i++;
  • 14. k++; } while (j < right_length) { a[k] = temp_right[j]; j++; k++; } } Output: Explanation: Here splits the list into small pieces, each with just one or no elements. It then puts these pieces back together in a sorted order. It keeps doing this until the whole list is sorted. So, first splits the whole list then it's sorted again 7. Counting Inversion: #include <iostream> using namespace std; int inversion_count(int arr[], int n) {
  • 15. int count = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) count++; } } return count; } int main() { int n; cout << "Array Size:"; cin >> n; int arr[n]; cout << "Array:"; for(int i=0; i<n; i++) { cin >> arr[i]; } cout << " Inversions Count:" << inversion_count(arr, n); return 0; }
  • 16. Output: Explanation: In the sorting process, we keep track of how many times a smaller number appears after a larger number, known as an "inversion." After completing the sorting, we calculate and provide the total count of inversions present in the array. This count helps us understand the complexity and order of elements in the sorted array.