Question

In: Computer Science

How would I add a quickSort function to the below C++ code to sort the randomly...

How would I add a quickSort function to the below C++ code to sort the randomly generated numbers?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int i;
int array[10];
int odd;
int Max;
int counter = 0;

int main()

{
cout << "The 10 random elements are: ";
cout << endl;

srand ( time(0) );

for (int j = 0; j < 99; j++)
{
    i = rand() % 100;
    if (i != i - 1)
    array[j] = i;

else
{
    i = rand() % 100;
    array[j] = i;
}


}

for (int k = 0; k < 10 ; k++)
{
    cout << array[k] << "\n";

}

cout << endl;


return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int i;
int array[10];
int odd;
int Max;
int counter = 0;

int partition(int *a,int start,int end)
{
int pivot=a[end];
//P-index indicates the pivot value index

int P_index=start;
int i,t; //t is temporary variable

for(i=start;i<end;i++)
{
   if(a[i]<=pivot)
{
t=a[i];
a[i]=a[P_index];
a[P_index]=t;
P_index++;
}
}
//Now exchanging value of
//pivot and P-index
t=a[end];
a[end]=a[P_index];
a[P_index]=t;

//at last returning the pivot value index
return P_index;
}
//function driver for quicksort
void quicksort_fun(int *a,int start,int end)
{
if(start<end)
{
int P_index=partition(a,start,end);
//from start to index
quicksort_fun(a,start,P_index-1);
//index to end
quicksort_fun(a,P_index+1,end);
}
}

int main()

{
cout << "The 10 random elements are: ";
cout << endl;

srand ( time(0) );

for (int j = 0; j < 99; j++)
{
i = rand() % 100;
if (i != i - 1)
array[j] = i;

else
{
i = rand() % 100;
array[j] = i;
}


}
cout<<"Before sorting : ";
for (int k = 0; k < 10 ; k++)
{
cout << array[k] << " ";

}
quicksort_fun(array,0,9);
cout<<"\nAfter sorting using quicksort: ";
for (int k = 0; k < 10 ; k++)
{
cout << array[k] << " ";

}
cout << endl;


return 0;
}


Related Solutions

How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
Hi, I need the HTML5 code for the below. About Me **Would like to add a...
Hi, I need the HTML5 code for the below. About Me **Would like to add a image of a plane or something related to travel here. Mia Jo I am taking this class to earn my Computer programmer CL1. Things I Like to Do: Spend time with family Traveling People Watch Places I Want to Go or Have Visited: Dubai- December'18 Enjoyed shopping and the desert safari the most. Cuba- August '18 Enjoyed learning about the culture and history. China-...
I need a randomized quicksort function written in c++ or java with dual pivots and a...
I need a randomized quicksort function written in c++ or java with dual pivots and a partition function
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would like to find another way to compile the program without using static_cast in my code. Thank you #include <iostream> #include <iomanip> using namespace std; class Population { private: int population, birth, death; public: Population() { population = 2; birth = 0; death = 0; } Population(int x, int y, int z) { if (x < 2) population = 0; else population = x; if...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
C++ How would I sort my output to evaluate it by sorting the column by it's...
C++ How would I sort my output to evaluate it by sorting the column by it's size? I didn't include my full program but here is the main.cpp where i'm supposed to sort the output by column size. //User Libraries #include <cstdlib> #include <ctime> #include <iostream> using namespace std; //User Libraries #include "Table.h" #include "Triangle.h" //Global Constants //Function Prototype void prntRow(RowAray *,int); void prntTab(Table *); void prntTri(Triangle *); //Execution Begins Here! int main(int argc, char** argv) { //Initialize the random...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT