Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IVE GOT THE CODE FOR QUESTIONS 1, 2, 3 and 4. I NEED HELP WITH QUESTIONS 5 AND 6. THANKS BACKGROUND Module 7 showed that

IVE GOT THE CODE FOR QUESTIONS 1, 2, 3 and 4. I NEED HELP WITH QUESTIONS 5 AND 6. THANKS

BACKGROUND

Module 7 showed that one way of comparing different algorithms for accomplishing the same task is complexity analysis. You will recall that in complexity analysis we express the time an algorithm takes to run as a function of the size of the input, and we used the big-Oh notation. For example, if an algorithm has a complexity of O(1), then it always runs in the same amount of time, no matter what the size of the input is; if it O(n), then the time it takes for the algorithm to run is proportional to the size of the input.

However, complexity analysis has a number of limitations. For example, big-Oh analysis concerns the worst case scenario. For example, some sorting algorithms with a complexity of O(n^2) often run considerably faster if the list that it receives as input is (almost) sorted; other sorting algorithms with a complexity of O(n^2) always take the same amount of time, no matter what state the list is in. Also, in big-Oh, we look at the dominant term in our calculation of the complexity of the algorithm. Thus, when we analyze an algorithm and discover that it runs in 75,312 + n time units, we still say that it has a complexity of O(n). It is therefore deemed to be better than an algorithm that run in .007 + n^2 time units, as this algorithm has a complexity of O(n^2). We also saw the rationale behind this: If n becomes sufficiently large, the other factors become insignificant.

Fortunately, there is another way to determine how long it takes for an algorithm to run, namely timing experiments. In a timing experiment, you actually implement the algorithm in a programming language, such as Java or C++, and simply measure how long it takes for the algorithm to run.

In the term project for this course, you are going to conduct a timing experiment and compare the results with the results you would get from a complexity analysis. We will compare Bubble Sort with Selection Sort.

In its least sophisticated form, bubble sort (http://en.wikipedia.org/wiki/Bubble_sort) works as follows:

Assuming that the list contains n elements.

Compare the first and the second element in the list, and swap them if the last element is smaller than the preceding one; otherwise, do nothing to this pair.

Now, compare second and third element t and swap them if the first of them is larger than the second; otherwise, do nothing to this pair.

Move on the next pair and continue the process until you reach the end of the list.

A little reflection will show that at the end of this iteration, the last element in the list is now the largest element in the list. The last element has bubbled to the top.

Now repeat the process but rather than going to the end of the list, stop when you reach n-1.

Now repeat the process again, but rather than going to the end of the list, stop when you reach n-2.

Keep repeating this until you reach 1.

The Wikipedia entry has a little simulation that shows how bubble sort works. The code looks something like:

bubbleSort(array A){ n = length(A); for(j = n; j > 0, j--) for(i = 1; i A[i] swap(A,i-1, i); } } }

swap obviously swaps the elements and can be defined as:

swap(A, pos1, pos2) { temp = A[pos1]; A[pos1] = A[pos2]; A[pos2] = temp; }

Another sort is selection sort (http://en.wikipedia.org/wiki/Selection_sort). We saw selection sort in the question in the sub-module on how to determine the complexity of an algorithm. Array A contains n elements, the elements to be sorted. The algorithm starts at the first position in the array and looks through the array for the smallest element. Once it reaches the end of the array, it puts that element in the first cell of the array. It then restarts the whole process from the second position in the array, and continues until the entire array has been sorted.

selection_sort(array A) { int i,j int iMin; for(j = 0; j

 for ( i = j+1; i  
 if (a[i]  
 iMin = i; 
 } 
 } 
 
 if ( iMin != j ) { 
 swap(a[j], a[iMin]); 
 } 
 } } If you like the Hungarian dancers, they perform selection sort at http://www.youtube.com/watch?v=Ns4TPTC8whw 

THE PROJECT

The purpose of the project is to perform a timing experiment. You are required to complete the following activities:

1/ Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then uses the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java or C#

2/ Repeat 1 but use selection sort this time.

1 and 2 are primarily intended to make sure that your algorithms work.

Once you are convinced your programs work, do the following

3/ Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following:

Initiate a variable running_time to 0

Create a for loop that iterates 1000 times.

In the body of the loop,

Create an array of n random integers

Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find the time

Use bubble sort to sort the array

Get the time and set this to end-time

Subtract start-time from end-time and add the result to total_time

Once the program has run, note

The number of items sorted

The average running time for each array (total_time/1000)

Repeat the process six times, using 50, 250 and 500 as the size of the array for each of the two algorithms.

4/ Repeat 3 using selection sort.

5/ Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information. Show both sort algorithms on the same graph for comparison.

6/ Write a one page document explaining the results, bearing in mind that both algorithms have a complexity of O(n^2) and what you know about complexity analysis. Use your knowledge of complexity analysis to explain your results.

Please submit:

1/Program code for 1

2/Program code for 2

3/Program code used in 3 and the results of the nine program runs

4/Program code used in 4 and the results of the nine program runs

5/The spreadsheet created in 5

6/The report. In the report, say which machine you ran the experiments on (type of processor, RAM, etc).

(1) BubbleSort.java:

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class BubbleSort {

public static void bubbleSort(int array[]) {

int n = array.length;

for (int i = 0; i

for (int j = 0; j

if (array[j]> array[j + 1]) {

swap(array, j);

}

}

}

}

public static void swap(int array[],int j) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

public static void main(String[] args) {

Random rand = new Random();

Scanner scanner = new Scanner(System.in);

int n;

System.out.print("Please enter a number: ");

n = scanner.nextInt();

int[] array = new int[n];

for (int i = 0; i

array[i] = rand.nextInt();

}

System.out.println("Array before sorting: " + Arrays.toString(array));

bubbleSort(array);

System.out.println("Array after sorting: " + Arrays.toString(array));

scanner.close();

}

}

Output:

image text in transcribed

(2) SelectionSort.java:

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class SelectionSort {

public static void selectionSort(int[] array) {

for (int i = 0; i

int currentMin = array[i];

int currentMinIndex = i;

for (int j = i + 1; j

if (currentMin> array[j]) {

currentMin = array[j];

currentMinIndex = j;

}

}

if (currentMinIndex != i) {

swap(array, i, currentMinIndex);

}

}

}

public static void swap(int[] array,int i,int minIndex) {

int min = array[minIndex];

array[minIndex] = array[i];

array[i] = min;

}

public static void main(String[] args) {

Random rand = new Random();

Scanner scanner = new Scanner(System.in);

int n;

System.out.print("Please enter a number: ");

n = scanner.nextInt();

int[] array = new int[n];

for(int i=0;i<>

array[i] = rand.nextInt();

}

System.out.println("Array before sorting: "+Arrays.toString(array));

selectionSort(array);

System.out.println("Array after sorting: "+Arrays.toString(array));

scanner.close();

}

}

Output:

image text in transcribed

//(Solution #3) BubbleSort.java

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class BubbleSort {

public static void bubbleSort(int array[]) {

int n = array.length;

for (int i = 0; i

for (int j = 0; j

if (array[j] > array[j + 1]) {

swap(array, j);

}

}

}

}

public static void swap(int array[],int j) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

public static void main(String[] args) {

Random rand = new Random();

Scanner scanner = new Scanner(System.in);

int n;

System.out.print("Please enter a number: ");

n = scanner.nextInt();

long totalTime = 0;

for(int count=0;count

int[] array = new int[n];

for (int i = 0; i

array[i] = rand.nextInt();

}

long start_time = System.nanoTime();

bubbleSort(array);

long end_time = System.nanoTime();

totalTime = totalTime+(end_time-start_time);

}

System.out.println("Average Time for 1000 times sort "+n+" elements :"+(totalTime/1000)+" Nano second");

scanner.close();

}

}

======================================================

//(Solution #4) SelectionSort.java

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class SelectionSort {

public static void selectionSort(int[] array) {

for (int i = 0; i

int currentMin = array[i];

int currentMinIndex = i;

for (int j = i + 1; j

if (currentMin > array[j]) {

currentMin = array[j];

currentMinIndex = j;

}

}

if (currentMinIndex != i) {

swap(array, i, currentMinIndex);

}

}

}

public static void swap(int[] array,int i,int minIndex) {

int min = array[minIndex];

array[minIndex] = array[i];

array[i] = min;

}

public static void main(String[] args) {

Random rand = new Random();

Scanner scanner = new Scanner(System.in);

int n;

System.out.print("Please enter a number: ");

n = scanner.nextInt();

long totalTime = 0;

for(int count=0;count

int[] array = new int[n];

for (int i = 0; i

array[i] = rand.nextInt();

}

long start_time = System.nanoTime();

selectionSort(array);

long end_time = System.nanoTime();

totalTime = totalTime+(end_time-start_time);

}

System.out.println("Average Time for 1000 times sort "+n+" elements using selection sort:"+(totalTime/1000)+" Nano second");

scanner.close();

}

}

image text in transcribedimage text in transcribed

Problems JavadocDeclaration SearchProgress terminated> BubbleSort [Java Application] CAProgram FilesJavaljre1 80 144 binljavaw.exe (Oct 26, 2017, 10-34:53 AM) Please enter a number: 6 1194607050, 2042829048, -1365441403, 1462452949, -2119433877, 1587571576] Array before sorting: Array after sorting: [-2119433877, -1365441403, -1194607050, 1462452949, 1587571576, 2042029048]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Discuss consumer-driven health plans.

Answered: 1 week ago