Daa Practical-1 Niraj Gajera - 23mca016: Data Size Name Best Avg Worst

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DAA PRACTICAL-1

NIRAJ GAJERA – 23MCA016

• Comparative Analysis

data size name best avg worst


5000 insertion iterative 0 0.2 0.04
10000 insertion iterative 0.001 0.081 0.156
20000 insertion iterative 0 0.32 0.623
100000 insertion iterative 0 7.32 9.449
5000 Selection Iterative 0.029 0.117 0.139
10000 Selection Iterative 0.116 0.487 0.583
20000 Selection Iterative 0.466 1.871 2.462
100000 Selection Iterative 7.028 20.97 21.788
5000 Bubble Iterative 0.059 0.135 0.163
10000 Bubble Iterative 0.234 0.583 0.663
20000 Bubble Iterative 0.936 2.388 2.614
100000 Bubble Iterative 14.599 40.855 39.788
• Bubble Sort has the most noteworthy normal time, showing it's less productive than the other two
calculations.
• Inclusion Sort and Choice Sort have moderately comparative execution, with Addition Sort having a
marginally way better normal time.
• The best-case scenarios for all calculations happen when the cluster is as of now sorted.
• The worst-case scenarios for all calculations happen when the cluster is sorted in invert arrange.
• As the number of components increments, the time taken by all calculations too increments,
highlighting their time complexity.

5000 Insertion Recursive 0 0.02 0.039


10000 Insertion Recursive 0 0.078 0.156
20000 Insertion Recursive 0 0.318 0.632
5000 Selection Recursive 0.029 0.117 0.139
10000 Selection Recursive 0.117 0.489 0.585
20000 Selection Recursive 0.462 1.89 2.2644
5000 Bubble Recursive 0.028 0.106 0.137
10000 Bubble Recursive 0.118 0.457 0.537
20000 Bubble Recursive 0.473 1.949 2.153

• Recursive executions of these sorting calculations tend to have longer normal times compared to their
iterative partners.
• Bubble Sort, in specific, incorporates a discernible increment in normal time as the number of
components develops.
• Inclusion Sort and Choice Sort have moderately comparable exhibitions, with Inclusion Sort having
somewhat superior normal times.
• The best-case scenarios for all calculations happen when the cluster is as of now sorted.
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
practical_1.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include "sort.hpp"
using namespace std;

//* Main Program


int main()
{
cout << endl;
for (int i = 0; i < 66; i++)
{
cout << "-";
}
cout << endl;
cout << setw(28) << "Name |" << setw(12) << "Best |" << setw(12) << "AVG |" << setw(12) << "WORST |" <<
endl;
for (int i = 0; i < 66; i++)
{
cout << "-";
}
cout << endl;
ii(5000);
ii(10000);
ii(20000);
ii(100000);
si(5000);
si(10000);
si(20000);
si(100000);
bi(5000);
bi(10000);
bi(20000);
bi(100000);

ir(5000);
ir(10000);
ir(20000);

sr(5000);
sr(10000);
sr(20000);

br(5000);
br(10000);
br(20000);

for (int i = 0; i < 58; i++)


{
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
cout << "-";
}
return 0;
}
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
Sort.cpp
#include <iostream>
using namespace std;
//? insertion sort
void insertion(int numbers[], int size)
{
int i, key, j;
for (i = 1; i < size; i++)
{
key = numbers[i];
j = i - 1;
while (j >= 0 && numbers[j] > key)
{
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
}
}
void insertionSort(int numbers[], int size)
{

if (size <= 1)
return;

insertionSort(numbers, size - 1);

int last = numbers[size - 1];


int j = size - 2;

while (j >= 0 && numbers[j] > last)


{
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = last;
}

//? selection Sort


void selection(int numbers[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = i; j < size; j++)
{
if (numbers[i] > numbers[j])
{
swap(numbers[i], numbers[j]);
}
}
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
}
}
void selectionSort(int numbers[], int pos, int size)
{
if (size == pos)
{
return;
}
for (int i = pos; i < size; i++)
{
if (numbers[pos] > numbers[i])
{
swap(numbers[pos], numbers[i]);
}
}
selectionSort(numbers, pos + 1, size);
}

//? Bubble Sort


void Bubble(int numbers[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (numbers[j] > numbers[j + 1])
{
swap(numbers[j], numbers[j + 1]);
}
}
}
}
void BubbleSort(int numbers[], int pos, int size)
{
if (pos == -1)
{
return;
}
for (int i = 0; i < pos; i++)
{
if (numbers[i] > numbers[i + 1])
{
swap(numbers[i], numbers[i + 1]);
}
}
BubbleSort(numbers, pos - 1, size);
}

void br(int size)


{
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
int *random = (int *)calloc(size, sizeof(int));
clock_t startTime;
clock_t endTime;
int i = 0;
{
fstream myFile("gen.txt", ios_base::in);
while (myFile >> random[i] && i < size)
{
i++;
}
startTime = clock();
BubbleSort(random, size, size);
endTime = clock();
double BAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
startTime = clock();
BubbleSort(random, size, size);
endTime = clock();
double BBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
startTime = clock();
BubbleSort(random, size, size);
endTime = clock();
double BWT = ((double)endTime - (double)startTime) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Bubble Recursive " << " |"<< setw(10) << BBT << " |" << setw(10) << BAT << " |" <<
setw(10) << BWT << " |" << endl;
}
}
void ir(int size)
{

int *random = (int *)calloc(size, sizeof(int));


clock_t startTime;
clock_t endTime;
int i = 0;
{

fstream iFile("gen.txt", ios_base::in);


i = 0;
while (iFile >> random[i] && i < size)
{
i++;
}
startTime = clock();
insertionSort(random, size);
endTime = clock();
double IAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
startTime = clock();
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
insertionSort(random, size);
endTime = clock();
double IBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
startTime = clock();
insertionSort(random, size);
endTime = clock();
double IWT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Insertion Recursive " << " |"<< setw(10) << IBT << " |" << setw(10) << IAT << " |" <<
setw(10) << IWT << " |" << endl;
}
}
void sr(int size)
{

int *random = (int *)calloc(size, sizeof(int));


clock_t startTime;
clock_t endTime;
int i = 0;
{

fstream sFile("gen.txt", ios_base::in);


i = 0;
while (sFile >> random[i] && i < size)
{
i++;
}
startTime = clock();
selectionSort(random, 0, size);
endTime = clock();
double SAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
startTime = clock();
selectionSort(random, 0, size);
endTime = clock();
double SBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
startTime = clock();
selectionSort(random, 0, size);
endTime = clock();
double SWT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Selection Recursive " << " |"<< setw(10) << SBT << " |" << setw(10) << SAT << " |"
<< setw(10) << SWT << " |" << endl;
}
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
}
void si(int size)
{

int *random = (int *)calloc(size, sizeof(int));


clock_t startTime;
clock_t endTime;
int i = 0;
{

fstream ssFile("gen.txt", ios_base::in);


i = 0;
while (ssFile >> random[i] && i < size)
{
i++;
}
startTime = clock();
selection(random, size);
endTime = clock();
double ISAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
startTime = clock();
selection(random, size);
endTime = clock();
double ISBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
startTime = clock();
selection(random, size);
endTime = clock();
double ISWT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Selection Iterative " << " |"<< setw(10) << ISBT << " |" << setw(10) << ISAT << " |"
<< setw(10) << ISWT << " |" << endl;
}
}
void ii(int size)
{

int *random = (int *)calloc(size, sizeof(int));


clock_t startTime;
clock_t endTime;
int i = 0;

fstream iiFile("gen.txt", ios_base::in);


i = 0;
while (iiFile >> random[i] && i < size)
{
i++;
}
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
startTime = clock();
insertion(random, size);

endTime = clock();

double IIAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;

startTime = clock();
insertion(random, size);
endTime = clock();
double IIBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
startTime = clock();
insertion(random, size);
endTime = clock();
double IIWT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Insertion Iterative " << " |"<< setw(10) << IIBT << " |" << setw(10) << IIAT << " |" <<
setw(10) << IIWT << " |" << endl;
cout <<"hello";
iiFile.close();
}
void bi(int size)
{

int *random = (int *)calloc(size, sizeof(int));


clock_t startTime;
clock_t endTime;
int i = 0;

fstream bbFile("gen.txt", ios_base::in);


i = 0;
while (bbFile >> random[i] && i < size)
{
i++;
}
startTime = clock();
Bubble(random, size);
endTime = clock();
double IBAT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
startTime = clock();
Bubble(random, size);
endTime = clock();
double IBBT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
for (int i = 0; i < size / 2; i++)
{
swap(random[i], random[size - 1 - i]);
}
DAA PRACTICAL-1
NIRAJ GAJERA – 23MCA016
startTime = clock();
Bubble(random, size);
endTime = clock();
double IBWT = ((double)(endTime - startTime)) / CLOCKS_PER_SEC;
cout << size<<" ";
cout << setw(22) << "Bubble Iterative " << " |"<< setw(10) << IBBT << " |" << setw(10) << IBAT << " |" <<
setw(10) << IBWT << " |" << endl;
}

You might also like