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
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
void mergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
combine(a, low, high, mid);
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
//random number seed
srand(time(NULL));
int l = 1;
const clock_t startclock = clock();
for (int i = 0; i < l; i++)
{
int a[100000];
for (int j = 0; j < 100000; j++)
a[i] = rand() % 10000;
int len = 100000;
mergeSort(a, 0, len - 1);
}
cout << "\nStart time for Mergesort: " << float(startclock) / CLOCKS_PER_SEC;
cout << "\nEnd time for Mergesort: " << float(clock()) / CLOCKS_PER_SEC;
cout << "\nTime taken for Mergesort: " << float(clock() - startclock) / CLOCKS_PER_SEC;
cout << "\n\n";
return 0;
}
Quicksort:
O(n log(n))
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* 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--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
int _tmain(int argc, _TCHAR* argv[])
{
//random number seed
srand(time(NULL));
int l = 1;
const clock_t startclock = clock();
for (int i = 0; i < l; i++)
{
int a[100000];
for (int j = 0; j < 100000; j++)
a[i] = rand() % 10000;
int len = 100000;
quickSort(a, 0, len - 1); //must do len -1 due to \0 at the end of every array
}
cout << "\nStart time for Quicksort: " << float(startclock) / CLOCKS_PER_SEC;
cout << "\nEnd time for Quicksort: " << float(clock()) / CLOCKS_PER_SEC;
cout << "\nTime taken for Quicksort: " << float(clock() - startclock) / CLOCKS_PER_SEC;
cout << "\n\n";
return 0;
}
Insertion Sort
Time Complexity: O(n)
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void insertionSort(int arr[], int n)
{
int temp;
for (int i = 1; i < n; i++)
{
for (int j = i; j >= 1; j--)
{
if (arr[j] < arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
else
break;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
int l = 1;
const clock_t startclock = clock();
for (int i = 0; i < l; i++)
{
int a[100000];
for (int j = 0; j < 100000; j++)
a[i] = rand() % 10000;
int len = 100000;
insertionSort(a, len);
}
cout << "Start time for Insertion Sort: " << float(startclock) / CLOCKS_PER_SEC;
cout << "\nEnd time for Insertion Sort: " << float(clock()) / CLOCKS_PER_SEC;
cout << "\nTime taken for Insertion Sort: " << float(clock() - startclock) / CLOCKS_PER_SEC;
return 0;
}
Selection Sort
Time Complexity: O(n^2)
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void selectSort(int arr[], int n)
{
//pos_min is short for position of min
int pos_min, temp;
for (int i = 0; i < n - 1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min = j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//How to make a random number in C++
srand(time(NULL));
int l = 1;
const clock_t startclock = clock();
for (int i = 0; i < l; i++)
{
int a[100000];
for (int j = 0; j < 100000; j++)
a[i] = rand() % 10000;
int len = 100000;
selectSort(a, len);
}
cout << "Start time for Seleciton Sort: " << float(startclock) / CLOCKS_PER_SEC;
cout << "\nEnd time for Selection Sort: " << float(clock()) / CLOCKS_PER_SEC;
cout << "\nTime taken for Section Sort: " << float(clock() - startclock) / CLOCKS_PER_SEC;
cout << "\n\n" << endl;
return 0;
}
Give a description and a comparison/analysis of the running times of different algorithms.
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...
How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private: float array[1000] ; int n = 1000; int i=0; public: void fillArray() { for(i=1;i<=n;i++) { array[i-1]= ( rand() % ( 1000) )+1; } } void arrayout () { ...
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...
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...
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++)...
graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...
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 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...
Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1]; } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...
Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the length and then calculate the average time to sort. Finally, compare the average run time for each length to the average time for the Merge Sort. -------------------------------------------- Initial python code: import random import time def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid...
#include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....