0% found this document useful (0 votes)
88 views4 pages

C++ Sorting and Searching Guide

The document defines functions for linear search, binary search, selection sort, bubble sort, and insertion sort. It generates random numbers, performs linear and binary searches before and after sorting, and times the operations. The results show the performance of different search algorithms before and after applying various sorting methods.

Uploaded by

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

C++ Sorting and Searching Guide

The document defines functions for linear search, binary search, selection sort, bubble sort, and insertion sort. It generates random numbers, performs linear and binary searches before and after sorting, and times the operations. The results show the performance of different search algorithms before and after applying various sorting methods.

Uploaded by

Ren Zx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <bits/stdc++.

h>
#include <iostream>
#include <cstdlib>
#include <chrono>

using namespace std;


using namespace std::chrono;

const int ARRAY_SIZE = 1000;

int linearSearch(const int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}

int binarySearch(const int arr[], int size, int target) {


int left = 0;
int right = size - 1;
int iterations = 0;

while (left <= right) {


iterations++;
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return iterations;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return iterations;
}

void selectionSort(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
std::swap(arr[i], arr[minIndex]);
}
}
}
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
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;
}
}

int main() {
int arr[ARRAY_SIZE];

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


arr[i] = rand() % 10000;
}

cout << "first 10 random elements " << endl;

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


cout << "Index " << i << ": " << arr[i] << endl;
}

int target;
int searchResult;
cout << "Enter a value to search for: ";
cin >> target;
cout<<endl;

auto startL = high_resolution_clock::now();


int linearIterations = linearSearch(arr, ARRAY_SIZE, target);
auto stopL = high_resolution_clock::now();
auto durationL = duration_cast<nanoseconds>(stopL-startL);
cout << "Linear Search Iterations : " << linearIterations<<endl;
cout << "linear search duration before sorting is : " << fixed <<
durationL.count()<<" sec " << endl;

if(searchResult == -1){
cout<<"Number was not found "<<endl; }
else{
cout<<"Number was found at: "<<searchResult << endl;
}
cout<<endl;

auto startb = high_resolution_clock::now();


int binaryIterationsBeforeSort = binarySearch(arr, ARRAY_SIZE, target);
auto stopb = high_resolution_clock::now();
auto durationb = duration_cast<nanoseconds>(stopb-startb);
cout << "Binary Search Iterations : " << binaryIterationsBeforeSort << endl;
cout << "binary search duration before sorting is : " << fixed <<
durationb.count()<<" sec " << endl;

if(searchResult == -1){

cout<<"NUmber was not found"<<endl; }


else{
cout<<"Number was found at : "<<searchResult << endl;
}
cout<<endl;

auto startbs = high_resolution_clock::now();


selectionSort(arr, ARRAY_SIZE);
searchResult = binarySearch(arr, ARRAY_SIZE, target);
auto stopbs = high_resolution_clock::now();
auto time_taken_selection = duration_cast<nanoseconds>(stopbs-startbs);
cout << "Binary Search after selection sort : " << time_taken_selection.count()
<< "sec" << endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl;
}
else{
cout<<"Number was found at : "<<searchResult << endl;
}
cout<<endl;

auto startbb = high_resolution_clock::now();


bubbleSort(arr , ARRAY_SIZE );
searchResult = binarySearch(arr, ARRAY_SIZE, target);
auto stopbb = high_resolution_clock::now();
auto time_taken_bubble = duration_cast<nanoseconds>(stopbb-startbb);
cout << "Binary search after bubble sort : " << fixed <<
time_taken_bubble.count() << setprecision(5) << " sec" << endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl; }
else{
cout<<"Number was found at :"<<searchResult << endl;
}
cout<<endl;

auto starti=high_resolution_clock::now();
insertionSort(arr, ARRAY_SIZE );
searchResult= binarySearch(arr, ARRAY_SIZE, target);
auto stopi = high_resolution_clock::now();
auto time_taken_insertion = duration_cast<nanoseconds>(stopi-starti);
cout<<"Binary search after insertion
sort :"<<fixed<<time_taken_insertion.count()<<"sec"<<endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl; }
else{
cout<<"Number was found at : "<<searchResult << endl;
}
cout<<endl;

auto startLs = high_resolution_clock::now();


selectionSort(arr, ARRAY_SIZE);
searchResult = linearSearch(arr, ARRAY_SIZE, target);
auto stopLs = high_resolution_clock::now();
auto durationLs = duration_cast<nanoseconds>(stopLs - startLs);
cout <<"Linear Search after selection sorting is : " << durationLs.count()<<"
sec " <<endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl;
}
else{
cout<<"Number was found at : "<<searchResult << endl<<endl;
}
cout<<endl;
auto startLi = high_resolution_clock::now();
insertionSort(arr, ARRAY_SIZE );
searchResult = linearSearch(arr, ARRAY_SIZE, target);
auto stopLi = high_resolution_clock::now();
auto durationLi = duration_cast<nanoseconds>(stopLi - startLi);
cout <<" Linear Search after insertion sorting is : " << durationLs.count()<<
"sec" <<endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl;
}
else{
cout<<"Number was found at : "<<searchResult << endl;

}
cout<<endl;
auto startLb = high_resolution_clock::now();
bubbleSort(arr , ARRAY_SIZE );
searchResult = binarySearch(arr, ARRAY_SIZE, target);
auto stopLb = high_resolution_clock::now();
auto durationLb = duration_cast<nanoseconds>(stopLb-startLb);
cout << "Linear search after bubble sort : " << fixed << durationLb.count() <<
" sec" << endl;

if(searchResult == -1){
cout<<"NUmber was not found"<<endl;
}
else{
cout<<"Number was found at : "<<searchResult << endl<<endl;
}

return 0;
}

You might also like