Você está na página 1de 4

Design and Analysis of Algorithm Assignment # 1 Asad Sarwar Reg # 873 BESE-3A

Task1 Implement the 3-sum algorithm and print out running times for 1k, 4k and 8k integers. You can generate these integers using a simple function that runs for a loop with the random function to generate these integers.
#include<iostream> #include<conio.h> #include<ctime> #include<stdlib.h> using namespace std; const int n=1000; int counter=0; void Three_sum(int three[]) //3-Sum function definition { for(int i=0; i<n; i++) //Adding consecutive three numbers in the array to check their sum { for(int j=i+1; j<n; j++) { for(int k=j+1; k<n; k++) { if(three[i]+three[j]+three[k]==0) { counter++; //counting the triples satisfying 3-Sum } } } } } void main() { int array1[n]; //Header files

//Declaring Array

srand(time(NULL)); for(int i=0; i<n; i++) //Initializing array with random numbers { array1[i]= -500 + rand()%1000; } clock_t start=clock(); //Start timer clock Three_sum(array1); //3-Sum call clock_t end=clock(); //End timer clock float time_used= float(end-start)/CLOCKS_PER_SEC; //Calculating total time used cout<< "Time taken for Three Sum to execute is: "<<time_used<<endl; cout<<" The total triples found = "<<counter<<endl; _getch(); //End of Main }

Task2 Implement the fast 3-sum algorithm and print out running times for 1k, 4k and 8k integers. You will need to use some sorting algorithm to sort your data and the binary search algorithm to search data.
#include<iostream> #include<conio.h> #include<stdlib.h> #include<ctime> using namespace std; const int n=8000; int counter=0; void Insertion_Sort(int sorted[]) //Insertion sort definition { int j; int key; int temp; for(int i=1;i<n;i++) { key=sorted[i]; j=i-1; while(j>=0 && sorted[j]>key) { sorted[j+1]=sorted[j]; j=j-1; } sorted[j+1]=key; } } int Binary_Search(int a[],int key,int min,int max) //Binary Search Implementation //header files

{ int midpoint; while(min<=max) { midpoint=(min+max)/2; if(key<a[midpoint]) { max=midpoint-1; } else if(key>a[midpoint]) { min=midpoint+1; } else { return midpoint; } } return -1; } void Fast_3Sum(int a[]) { int positive,negative; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { positive=a[i]+a[j]; negative=Binary_Search(a,-positive,0,n-1); if(positive==-a[negative]) { counter++; } } } } void fill(int a[]) //Initializing array with random umber { for(int i=0; i<n; i++) { a[i]= -500 + rand()%1000; } } void main() { int array1[n]; srand(time(NULL)); fill(array1);

//Filling array

Insertion_Sort(array1); clock_t start=clock(); Fast_3Sum(array1); //Fast 3-Sum Function call clock_t end=clock(); float time_used = float(end-start)/CLOCKS_PER_SEC; cout<<"The time used in execution of Fast 3-Sum : " <<time_used<<endl; cout<<"Total possible triples ="<<counter<<endl; _getch(); }

Table:
Size (N) 3-Sum Fast 3-Sum 1K
0.537 0.075

2K
4.144 0.311

3K
13.871 0.651

4K
32.768 1.12

5K
71.205 1.714

8K
261.626 4.21

Graph:
X-axis Array Size (N) Y-Axis Time
300 250 200 150 100 50 0 0 2000 4000 Array Size 6000 8000 10000 3-Sum Fast 3-Sum

Você também pode gostar