You are on page 1of 2

Assignment 1: Analysing 3-sum and Fast 3-sum algorithms

Objectives
The objective of this assignment is to compare the running time complexity of 3-sum and fast 3-
sum algorithms.
Description
The 3-sum is a simple algorithm that finds triplets in an array that sum to 0. The brute force
method to solve this problem uses triple for loops to find all combinations. The complexity for
this method is high i.e. the order of N
3
. The fast 3-sum algorithm is an improvement that uses
sorting and binary search to find triplets that sum to 0. The fast 3-sum improves on the 3-sum
algorithm in terms of complexity.
Task 1

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 a for loop with the random function to
generate these integers.

Task 2

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.

Table

Running times for input data in seconds
1k 4k 8k
3-sum


Fast 3-sum



Fill in the above table and plot a graph to show the comparisons of running times of the two
algorithms.

Note
You can measure the efficiency of the algorithms by calculating the time it takes to execute the
algorithm. You can use a built-in C++ function called clock() to compute the time.
#include <ctime> // header file for clock() and CLOCKS_PER_SEC
...
clock_t start = clock(); // start timer
<Your code comes here>
clock_t stop = clock(); // stop timer
cpu_time_used = float(stop-start)/CLOCKS_PER_SEC; //time in seconds

Deliverable
You are required to upload the assignment as Assign1_your_name.doc on LMS.

You might also like