Question

The goal is to generate some random number and output a sorted list by quicksort. what...

The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you


#include "pch.h"
#include <iostream>

using namespace std;
/* C implementation QuickSort */
#include<stdio.h>
void swap(int* a, int* b)
{
   int t = *a;
   *a = *b;
   *b = t;
}
int partition(int arr[], int low, int high)
{
   int pivot = arr[high]; // pivot
   int i = (low - 1); // Index of smaller element

   for (int j = low; j <= high - 1; j++)
   {
  
       if (arr[j] <= pivot)
       {
           i++;   
           swap(&arr[i], &arr[j]);
       }
   }
   swap(&arr[i + 1], &arr[high]);
   return (i + 1);
}


void quickSort(int arr[], int low, int high)
{
   if (low < high)
   {
      
       int pi = partition(arr, low, high);


       quickSort(arr, low, pi - 1);
       quickSort(arr, pi + 1, high);
   }
}
void printArray(int arr[], int size)
{
   int i;
   for (i = 0; i < size; i++)
       printf("%d ", arr[i]);
   printf("n");
}
int main()
{
   const unsigned int Arraysize =6;
   int numbArray[Arraysize];
  
   for (int i = 0; i < Arraysize; i++)
   {
       numbArray[i] = rand() % 1000;
       cout << numbArray[i] << endl;
   }
   return 0;
}

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

//#include "pch.h"
#include <iostream>

using namespace std;
/* C implementation QuickSort */
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++)
{
  
if (arr[j] <= pivot)
{
i++;   
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}


void quickSort(int arr[], int low, int high)
{
if (low < high)
{
  
int pi = partition(arr, low, high);


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
const unsigned int Arraysize =6;
int numbArray[Arraysize];
  
for (int i = 0; i < Arraysize; i++)
{
numbArray[i] = rand() % 1000;
//cout << numbArray[i] << endl;
}
//display the array elements before sorting
cout<<endl<<"Before sorting the array is : ";
//call to printArray()
printArray(numbArray,Arraysize);
//call to quicksort()
quickSort(numbArray,0,Arraysize-1);
cout<<endl<<"Sorted Array is : ";
//call to printArray()
printArray(numbArray,Arraysize);
return 0;
}

OUTPUT

iN BEST CASE IT WILL TAKE O(nlogn) and in worst case it will take O(n2)

 
Add a comment
Know the answer?
Add Answer to:
The goal is to generate some random number and output a sorted list by quicksort. what...
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
  • 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...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • I am trying to implement this version of quicksort in C/c++ but I am getting stuck....

    I am trying to implement this version of quicksort in C/c++ but I am getting stuck. Below is how the algorithm is supposed to work. This is the partitioning scheme I am trying to implement. 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ start 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ move left pointer to first element larger than pivot. 3 compares 17, -10, 7, 19, 21, 23, -13, 31, 59...

  • 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;...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • 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...

  • ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...

    ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...

  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • 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'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

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