Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
Check Mark
Knowledge Booster
Background pattern image
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.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education