You are on page 1of 16

Guide Chaitra Desai

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

Sort Bubble Sort

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();

char ch=cin.get(); return 0;

Observations
Sorting Technique Processor Used
Intel Core i7 2630QM Intel Pentium 4

Clock Ticks For Input Size of 10000

Clock Ticks For Input Size of 20000

Clock Ticks For Input Size of 30000

63 140 109 58 140 281 203 111

219 579 422 230 624 1141 844 443

515 1313 969 523 1466 2953 2078 1006

Selection Sort

AMD Athlon X2 3600+ AMD Phenom II X4 970 BE Intel Core i7 2630QM

Bubble Sort

Intel Pentium 4 AMD Athlon X2 3600+


AMD Phenom II X4 970 BE

Analysis Of Selection Sort


3
2.5 2 1.5 1 1.046 1.03 1.938 n = 10000 n = 20000 n = 30000 2.626

0.5
0 AMD Phenom II X4 BE Intel Core i7 2630QM AMD Athlon X2 Intel Pentium 4 3600+
Scale :

Y : 1 Division = 500 Clock Ticks

Analysis Of Bubble Sort


7 6
5 4 3 2 1 0 AMD Phenom II X4 BE Intel Core i7 2630QM AMD Athlon X2 3600+ Intel Pentium 4
Scale :
Y : 1 Division = 500 Clock Ticks

5.906
4.156 2.932 2.012 n = 10000 n = 20000 n = 30000

AMD Phenom II X4 970 BE

Intel Core i7 2630QM

AMD Athlon X2 3600+

Intel Pentium 4

Conclusion

According to the above observations made we can conclude the following:


An

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 !

You might also like