Sorting Agorithm - Assignment No 1 Sorting Agorithm - Assignment No 1
Adil Khan 6 years ago

Sorting Agorithm - Assignment No 1

Q1. Using any two sorting algorithms, sort arrays of length 100, 1000, 10000. Calculate the time taken by the algorithm for sorting the arrays of given lengths. Ans:     Selection Sort:

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.

Program:

#include <iostream>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
void Min(int *a,int &k,int n,int &loc){
   int mini = a[k];
   loc = k;
   for(int j=k+1;j<n;j++){
       if(mini > a[j]){
           mini = a[j];
           loc = j;
       }
   }
}
int main()
{
   int n = 1000; // size of array it can be changed
   int k=0,loc=0,temp,i;
   int a[n];
   clock_t sTime, eTime;
   // filling array with random numbers
   for(i=0;i<n;i++){
       int val = rand()%1000;
       a[i] = val;
   }
   // printing unsorted array
   cout<<"Random Numbers Array "<<endl;
   for(i=0;i<n;i++){
           cout<<a[i]<<" ";
   }
   cout<<endl<<endl<<endl;
   // sorting array with selection sort
   cout<<"Sorting Array Using Selection Sort... "<<endl;
   sTime = clock();
   //selection sort start
   for(k=0;k<n;k++){
       Min(a,k,n,loc);
       temp = a[k];
       a[k] = a[loc];
       a[loc] = temp;
   }
   //selection sort end
   eTime = clock();
   double time_taken = double(eTime - sTime) / double(CLOCKS_PER_SEC);
   cout << "Array of "<<n<<" numbers sorted in "
           << fixed<< time_taken << setprecision(5);
   cout << " sec " << endl<<endl<<endl;
   for(i=0;i<n;i++){
           cout<<a[i]<<" ";
   }
   return 0;
}

 

Outputs:

  1. Array of 100 Numbers

In the figure, time taken for 100 numbers is so much short that’s why it is 0.000000 sec.

Figure 1 Sorting 100 Numbers

  1. Array of 1000 Numbers

In the figure, time taken for 1000 numbers is 0.001000 sec.

Sorting Agorithm - Assignment No 1 _ 1.zip

Download: Sorting Agorithm - Assignment No 1 _ 1.zip

0
1.8K
Quality inspection of cereal grains using computer vision techniques

PROJECT SUMMARY: Great varieties of foods are attainable in grain structure and are essent...

1675638330.png
Adil Khan
1 year ago
IMPROVEMENT IN POWER SYSTEM PROTECTION WITH SCADA SYSTEM VERIFICATION

Modern power stations are more complex and flexible than the conventional power systems. F...

1675638330.png
Adil Khan
1 year ago
automatic Control Shed Using PLC

In manual control shed, 24/7 monitoring of the shed is impossible, and chickens require a...

1675638330.png
Adil Khan
1 year ago
Smart Dustbin

to implement a smart dustbin built on a microcontroller based platfrom Arudino Uno board...

1675638330.png
Adil Khan
1 year ago
IOTS PATIENT MONITORING BASED SYSTEM

Day by day ratio of death is increasing and the major cause behind this is heart attack .H...

1675638330.png
Adil Khan
1 year ago