Professional Documents
Culture Documents
Anup V Shanbhag 2BV10IS019 Rahul Hosmani 2BV10IS0 Sachin Vaidya 2BV10IS081 Akhilesh Kumar 2BV10IS007 Akashdeep Pal 2BV10IS006 BVB College of Engineering & Technology, Hubli-31, Karnataka, India
18th May, 2012
DAA ACTIVITY.
Objective
Objective of this activity is to study the effect of change in input size on the algorithms execution time, on different systems (High End & Low End). Algorithms Under Consideration Are :
Selection
Selection Sort
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. The algorithm works as follows: Find the minimum value in the list. Swap it with the value in the first position. Repeat the steps above for the remainder of the list (Starting at the second position and advancing each time).
CODE:
void SelectionSort(int arr[],int n) { int minIndex,tmp; for (int i=0;i<n-1;i++) { minIndex=i; for (int j=i+1;j<n;j++) { if (arr[j]<arr[minIndex]) minIndex=j; } tmp=arr[i]; arr[i]=arr[minIndex]; arr[minIndex]=tmp; } }
Bubble Sort
Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. The algorithm works as follows: Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. If at least one swap has been done, repeat step 1.
CODE:
void BubbleSort(int arr[], int n) { int tmp; for(int i=0;i<n-1;i++) { for(int j=0;j<(n-1-i);j++) if (arr[j+1]<arr[j]) { tmp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=tmp; } } }
#include <iostream> #include "sort.h" /*sort.h contains SelectionSort() And BubbleSort() Functions*/ #include <stdio.h> #include <ctime> using namespace std; int main(int argc, char *argv[]) { ofstream fout; clock_t start,stop; int a[30000],b[30000]; int n=30000; /* Input is given as 10000,20000,30000 */ unsigned int seed; seed=(unsigned)time(&t); srand(seed); for(int i=0;i<n;i++) a[i]=(rand()%n)+1;
seed=(unsigned)time(&t); srand(seed); for(int i=0;i<n;i++) b[i]=(rand()%n)+1; start=clock(); /* Function To Calculate Clock Ticks since Start Of Program */ SelectionSort(a,n); stop=clock(); fout.open("selsort.txt"); fout<<(stop-start); fout.close(); start=clock(); BubbleSort(b,n); stop=clock(); fout.open("bubsort.txt"); fout<<(stop-start); fout.close();
Observations
Sorting Technique Processor Used
Intel Core i7 2630QM Intel Pentium 4
Selection Sort
Bubble Sort
0.5
0 AMD Phenom II X4 BE Intel Core i7 2630QM AMD Athlon X2 Intel Pentium 4 3600+
Scale :
5.906
4.156 2.932 2.012 n = 10000 n = 20000 n = 30000
Intel Pentium 4
Conclusion
Algorithms Execution Time Increases With An Increase in its Input Size. In Other words the efficiency of a n algorithm depends upon the input parameter. An Algorithms efficiency is greatly influenced by the system on which its run. Faster the systems are, faster is the execution of the algorithm.
Thank You !