include using namespace std; void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int binarySearch(int arr[], int l, int r, int x, int& comp) { comp++; if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) { return mid; } if (arr[mid] > x) { return binarySearch(arr, l, mid - 1, x, comp); } return binarySearch(arr, mid + 1, r, x, comp); } return -1; } int main() { int Num[8192]; srand(time(NULL)); for (int i = 0; i < 8192; i++) { Num[i] = rand() % 10001; } clock_t starting_time = clock(); bubbleSort(Num, 8192); clock_t ending_time = clock(); clock_t result = ending_time - starting_time; print_Title(); cout << setprecision(2) << fixed; cout << "Bubble sort takes " << (double)result/CLOCKS_PER_SEC << " seconds. \n"; cout << "Bubble sort takes " << (double)result << " milliseconds. \n\n"; int x = 71, comparisons = 0; int index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 9997; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 5000; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 2; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 3015; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } return 0; } implement a selection sort function that measures the time in seconds and milliseconds like how bubble sort was done
include using namespace std; void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int binarySearch(int arr[], int l, int r, int x, int& comp) { comp++; if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) { return mid; } if (arr[mid] > x) { return binarySearch(arr, l, mid - 1, x, comp); } return binarySearch(arr, mid + 1, r, x, comp); } return -1; } int main() { int Num[8192]; srand(time(NULL)); for (int i = 0; i < 8192; i++) { Num[i] = rand() % 10001; } clock_t starting_time = clock(); bubbleSort(Num, 8192); clock_t ending_time = clock(); clock_t result = ending_time - starting_time; print_Title(); cout << setprecision(2) << fixed; cout << "Bubble sort takes " << (double)result/CLOCKS_PER_SEC << " seconds. \n"; cout << "Bubble sort takes " << (double)result << " milliseconds. \n\n"; int x = 71, comparisons = 0; int index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 9997; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 5000; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 2; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } x = 3015; comparisons = 0; index = binarySearch(Num, 0, 8191, x, comparisons); if (index != -1) { cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl; } else { cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl; } return 0; } implement a selection sort function that measures the time in seconds and milliseconds like how bubble sort was done
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
#include<bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int binarySearch(int arr[], int l, int r, int x, int& comp) {
comp++;
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] > x) {
return binarySearch(arr, l, mid - 1, x, comp);
}
return binarySearch(arr, mid + 1, r, x, comp);
}
return -1;
}
int main() {
int Num[8192];
srand(time(NULL));
for (int i = 0; i < 8192; i++) {
Num[i] = rand() % 10001;
}
clock_t starting_time = clock();
bubbleSort(Num, 8192);
clock_t ending_time = clock();
clock_t result = ending_time - starting_time;
print_Title();
cout << setprecision(2) << fixed;
cout << "Bubble sort takes " << (double)result/CLOCKS_PER_SEC << " seconds. \n";
cout << "Bubble sort takes " << (double)result << " milliseconds. \n\n";
int x = 71, comparisons = 0;
int index = binarySearch(Num, 0, 8191, x, comparisons);
if (index != -1) {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl;
}
else {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl;
}
x = 9997; comparisons = 0;
index = binarySearch(Num, 0, 8191, x, comparisons);
if (index != -1) {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl;
}
else {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl;
}
x = 5000; comparisons = 0;
index = binarySearch(Num, 0, 8191, x, comparisons);
if (index != -1) {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl;
}
else {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl;
}
x = 2; comparisons = 0;
index = binarySearch(Num, 0, 8191, x, comparisons);
if (index != -1) {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl;
}
else {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl;
}
x = 3015; comparisons = 0;
index = binarySearch(Num, 0, 8191, x, comparisons);
if (index != -1) {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - found." << endl;
}
else {
cout << "Binary search " << x << " takes " << comparisons << " comparisons - not found." << endl;
}
return 0;
}
implement a selection sort function that measures the time in seconds and milliseconds like how bubble sort was done.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education