Question

I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort

here is my code:

But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me to add this part?

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
static int count = 0;

int quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
//int count =0;
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
count++;
}
};

/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
return count;
}
int selectSort(int arr[], int n) {

int index_min_value;
int temp;
int count=0;
for (int i=0; i<n-1; i++) {
// outer loop tracks the sorted portion of the list
index_min_value = i; //set index_min_value to the current index of array
for (int j=i+1; j < n; j++) // inner loop finds the next smallest value
{
if (arr[j] < arr[index_min_value])
index_min_value = j;


}
if (index_min_value != i) {
temp = arr[i];
arr[i] = arr[index_min_value];
arr[index_min_value] = temp;
count++;
}
}
return count;
}
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void printList(int arr[], int length){
for (int i = 0; i<length; i++){
cout<<arr[i]<<" ";

}
cout<<endl;
}

int main(){
//HINT: You should place a counter into each algorithm to see how many time each of them run. Then you can compare them easier.
//You should use the given print statements for better organization.
int count = 0;
int myArray1[] = {12, 13, 5, 4, 7, 18, 9 };
int myArray2[] = {12, 13, 5, 4, 7, 18, 9 };
//selectSort
int start = clock();
count = selectSort(myArray1,7);
int stop = clock();


cout<<"When ordered with selectSort, after "<<count<<" operations the result is: "<<endl;
cout<< "time = " <<(stop-start)/double(CLOCKS_PER_SEC)*1000 << endl;
printList(myArray1,7);
int start1 = clock();
count = quickSort(myArray2,0,6);
int stop1 = clock();


cout<<"When ordered with quickSort, after "<<count<<" operations the result is: "<<endl;
cout<< "time1 = " <<(stop1-start1)/double(CLOCKS_PER_SEC)*1000 << endl;
printList(myArray2,7);
int *myArray3 = new int[10000000];
srand((unsigned)time(0));

for(int i=0; i<1000000; i++){
myArray3[i] = (rand()%10000)+1;

//cout << myArray5[i] << endl;
}
int start2 = clock();
count = selectSort(myArray3,1000000);
int stop2 = clock();
cout<<"When ordered with selectSort, after "<<count<<" operations the result is: "<<endl;
cout<< "time = " <<(stop2-start2)/double(CLOCKS_PER_SEC)*1000 << endl;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PAvck= arr[high):

Add a comment
Know the answer?
Add Answer to:
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function...

    quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4. So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process "There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase) class quicksort { private: int left; int right; int* array;   ...

  • I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

  • Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace...

    Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() {     ifstream fin("random.dat", ios::binary);     for (long i = 0; i < length; i++)     {         fin.read((char*)&list[i], sizeof(int));     }     fin.close(); } void bubbleSort() {     int temp;     for(long i = 0; i < length; i++)     {         for(long j = 0; j< length-i-1; j++)...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT