Part 2 and 3 For part 2 Please the code in c languageSolutio
Part 2 and 3
Solution
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int i = 100;
const long n = 300000;
int list[n];
void read()
{
ifstream fin(\"random.data\", i::binary);
for (int x = 0; x < i; x++)
{
fin.read((char*)&list[x], sizeof(int));
}
fin.close();
}
void bubbleSort()
{
int temp;
for(long x = 0; x < i; x++)
{
for(long j = 0; j < i-x-1; j++)
{
if (list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}
void insertionSort()
{
int temp;
for(long x = 1; x < i; x++)
{
temp = list[x];
long j;
for(j = x-1; j >= 0 && list[j] > temp; j--)
{
list[j+1] = list[j];
}
list[j+1] = temp;
}
}
long partition(long left, long right)
{
int pivot_element = list[left];
int lb = left, ub = right;
int temp;
while (left < right)
{
while(list[left] <= pivot_element)
left++;
while(list[right] > pivot_element)
right--;
if (left < right)
{
temp = list[left];
list[left] = list[right];
list[right] = temp;
}
}
list[lb] = list[right];
list[right] = pivot_element;
return right;
}
void quickSort(long left, long right)
{
if (left < right)
{
long pivot = partition(left, right);
quickSort(left, pivot-1);
quickSort(pivot+1, right);
}
}
int main()
{
double t1, t2;
for (i = 1000; i <= n; )
{
cout << \"\ Length\\t: \" << length << \'\ \';
read();
t1 = clock();
bubbleSort();
t2 = clock();
cout << \"Bubble Sort\\t: \" << (t2 - t1)/CLK_TCK << \" sec\ \";
read();
t1 = clock();
insertionSort();
t2 = clock();
cout << \"Insertion Sort\\t: \" << (t2 - t1)/CLK_TCK << \" sec\ \";
read();
t1 = clock();
quickSort(0, length - 1);
t2 = clock();
cout << \"Quick Sort\\t: \" << (t2 - t1)/CLK_TCK << \" sec\ \";
}
return 0;
}



