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 file will be like random size of the array and next to it the time it took for each size of the random array for it so i can make a graph with time and size for quick sort
code c++
//System Libraries
#include<stdio.h>
#include <ctime>
#include <cmath>
#include <bits/stdc++.h>
#include<iostream>
#include <cstdlib>
using namespace std;
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in
sorted
array, and places all smaller (smaller than
pivot)
to left of pivot and all greater elements to
right
of pivot */
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 current element is
smaller than or
// equal to pivot
if (arr[j] <=
pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
time_t start,end;
float timeSp;
start=clock();
if (low < high)
{
/* pi is partitioning
index, arr[p] is now
at
right place */
int pi = partition(arr,
low, high);
// Separately sort
elements before
// partition and after
partition
quickSort(arr, low, pi -
1);
quickSort(arr, pi + 1,
high);
}
end=clock();
timeSp=float(start-end)/CLOCKS_PER_SEC;
cout<<"Time spent= "<< timeSp
<<endl;
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ",
arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
Give the size in large so you can get time.......
/**********************sort.cpp**********************/
#include<stdio.h>
#include <ctime>
#include <cmath>
#include <bits/stdc++.h>
#include<iostream>
#include <cstdlib>
using namespace std;
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
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 current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[100000];
int n = sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++){
arr[i] = rand() % 10000 + 1;
}
time_t start,end;
float timeSp;
start=clock();
quickSort(arr, 0, n-1);
end=clock();
timeSp=float(end-start)/CLOCKS_PER_SEC;
cout<<"Time spent= "<< timeSp <<endl;
// printf("Sorted array: \n");
//printArray(arr, n);
return 0;
}
/**************output***********************/
Please let me know if you have any problem or you want to modify the answer, thanks :)
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...
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) { ...
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'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...
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...
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 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: 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...
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 { ...
Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...
C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #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; song++){ // Allocate memory for each individual character string database[song] =...