DAAFINAL
DAAFINAL
Step 1: Declare the variables and input all elements of an array in sorted order (ascending or
descending).
Step 3: Now compare the target elements with the middle element of the array. And if the
value of the target element is matched with the middle element, return the middle element's
position and end the search process.
Step 4: If the target element is less than the middle element, we search the elements into the
lower half of an array.
Step 5: If the target element is larger than the middle element, we need to search the element
into the greater half of the array.
Step 6: We will continuously repeat steps 4, 5, and 6 till the specified element is not found in
the sorted array.
PROGRAM :-
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
// declaration of the variables and array
int arr[100], st, mid, end, i, num, tgt;
cout << " Define the size of the array: " << endl;
cin >> num; // get size
}
// initialize the starting and ending variable's values
st = 0;
end = num - 1; // size of array (num) - 1
// define the item or value to be search
cout << " Define a value to be searched from sorted array: " << endl;
cin >> tgt;
// use while loop to check 'st', should be less than equal to 'end'.
while ( st <= end)
{
// get middle value by splitting into half
mid = ( st + end ) / 2;
/* if we get the target value at mid index, print the position and exit from the program.
*/
if (arr[mid] == tgt)
{
cout << " Element is found at index " << (mid + 1);
exit (0); // use for exit program the program
}
// check the value of target element is greater than the mid element' value
else if ( tgt > arr[mid])
{
st = mid + 1; // set the new value for st variable
}
// check the value of target element is less than the mid element' value
else if ( tgt < arr[mid])
{
end = mid - 1; // set the new value for end variable
}
}
cout << " Number is not found. " << endl;
return 0;
}
OUTPUT:-
Define the size of the array:
10
Enter the values in sorted array either ascending or descending order:
Arr [0] = 12 Arr [1] = 24 Arr [2] = 36
Arr [3] = 48 Arr [4] = 50 Arr [5] = 54
Arr [6] = 58 Arr [7] = 60 Arr [8] = 72
Arr [9] = 84
Define a value to be searched from sorted array:
50
Element is found at index 5
Step 2: Compare the search element with the first element in the list.
Step 4: Else compare the search element with the next element in the list.
PROGRAM :-
// Linear Search in C++
#include <iostream>
using namespace std;
int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 1;
int n = sizeof(array) / sizeof(array[0]);
(result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
}
OUTPUT:-
Element found at index: 1
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move
to the next element. Else, shift greater elements in the array towards the right.
PROGRAM :-
#include <bits/stdc++.h>
using namespace std;
arr[j + 1] = key;
}
}
insertionSort(arr, N);
printArray(arr, N);
return 0;
}
OUTPUT:-
5 6 11 12 13
1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort
PROGRAM :-
#include <bits/stdc++.h>
using namespace std;
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
{
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
}
int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
OUTPUT:-
Sorted array:
11 12 22 25 34 64 90
1. SELECTION SORT(arr, n)
2. Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
3. Step 2: CALL SMALLEST(arr, i, n, pos)
4. Step 3: SWAP arr[i] with arr[pos]
5. [END OF LOOP]
6. Step 4: EXIT
7. SMALLEST (arr, i, n, pos)
8. Step 1: [INITIALIZE] SET SMALL = arr[i]
9. Step 2: [INITIALIZE] SET pos = i
10. Step 3: Repeat for j = i+1 to n
11. if (SMALL > arr[j])
12. SET SMALL = arr[j]
13. SET pos = j
14. [END OF if]
15. [END OF LOOP]
16. Step 4: RETURN pos
PROGRAM :-
// C++ program for implementation of
// selection sort
#include <bits/stdc++.h>
using namespace std;
min_idx = i;
EN21CS301878, YAMAN KUMAR SAHOO
9
// Function Call
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
OUTPUT:-
Sorted array:
11 12 22 25 64
PROGRAM :-
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;
// left = 2*i + 1
int l = 2 * i + 1;
// right = 2*i + 2
int r = 2 * i + 2;
if (largest != i) {
swap(arr[i], arr[largest]);
EN21CS301878, YAMAN KUMAR SAHOO
11
// Function call
heapSort(arr, N);
OUTPUT:-
Sorted array is
5 6 7 11 12 13
PROGRAM :-
// C++ implementation of Radix Sort
#include <iostream>
using namespace std;
// Output array
int output[n];
int i, count[10] = { 0 };
// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
OUTPUT:-
PROGRAM :-
// C++ program to sort an
// array using bucket sort
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Index in bucket
int bi = n * arr[i];
b[bi].push_back(arr[i]);
}
int main()
{
float arr[]
= { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 };
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);
OUTPUT:-
Sorted array is
0.1234 0.3434 0.565 0.656 0.665 0.897
PROGRAM :-
// C++ program for Merge Sort
#include <bits/stdc++.h>
using namespace std;
<= rightArray[indexOfSubArrayTwo]) {
array[indexOfMergedArray]
= leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else {
array[indexOfMergedArray]
= rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}
while (indexOfSubArrayOne < subArrayOne) {
array[indexOfMergedArray]
= leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while (indexOfSubArrayTwo < subArrayTwo) {
array[indexOfMergedArray]
= rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
delete[] leftArray;
delete[] rightArray;
}
// Driver code
int main()
EN21CS301878, YAMAN KUMAR SAHOO
18
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
OUTPUT:-
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
PROGRAM :-
// C++ Implementation of the Quick Sort Algorithm.
#include <iostream>
using namespace std;
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
// Giving pivot element its correct position
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
// base case
if (start >= end)
return;
int main()
{
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
return 0;
}
OUTPUT:-
1 2 3 4 8 9