PLEASE DO BOTH #5 AND #6.
The purpose of the project is to perform a timing experiment. You are required to complete the following activities:
1 and 2 are primarily intended to make sure that your algorithms work.
Once you are convinced your programs work, 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 (Make sure you make the range of the random numbers substantially bigger than the array, ie. if the array size is 500 have the random number generator pick numbers between 1 and 5000. For the largest array have the random number generator pick numbers between 1 and 50,000).
Get the time and set this to start-time (notice the sort is started after each array is built. You want to time the srt process only). You will have to figure out what the appropriate command is in the programming language you are using to find the time (Important: Do not start the timer until after the array is created).
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 using 500, 2500 and 5000 as the size of the array.
5.) You now have 6 data points ( the averages from the three array sizes for the two sort algorithms) 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.
Below is the code which does all the sorting. I have named the
file Test.java.
Here is the comparison data(You can recreate similar data by
running the code and getting the data points):
Here are the key points to note when submitting the report:
Worst case time complexity: (n2)
Number of comparisons: (n2)
Best case time complexity :
Bubble sort: (n)
Selection sort: (n2)
Average case time complexity :
Bubble sort: (n2)
Selection sort: (n2)
In the bubble sort, each element and its adjacent element is
compared and swapped if required. On the other hand, selection sort
works by selecting the element and swapping that particular element
with the last element. The selected element could be largest or
smallest depending on the order i.e., ascending or
descending.
The worst case complexity is same in both the algorithms, i.e.,
O(n2), but best complexity is different. Bubble sort takes an order
of n time whereas selection sort consumes an order of n2
time.
Bubble sort is a stable algorithm, in contrast, selection sort is
unstable.
Selection sort algorithm is fast and efficient as compared to
bubble sort which is very slow and inefficient.
5. Selection sort swaps elements "n" times in worst case, but
Bubble sort swaps almost n*(n-1) times.
6. If we have a system where write operations are extremely
expensive and read operations are not, then Selection sort could be
ideal.
Selection sort is good for sorting arrays of small size.
7. Selection sort is better than Bubble sort due to less
swapping required.
8. In Bubble sort, we can identify whether list is sorted or not in
1st iteration but in Selection sort we can't able to identify
that.
Compared to Selection sort, Bubble sort should be used when the
given array is almost sorted.
The results were obtained on a Linux Machine with Intel i7
processor, 4 GB RAM.
If you want to change the number of arrays(500,2500,5000,etc), just change the value of variable number_of_iterations in Test.java.
The program asks the user for number of elements to sort.
Test.java:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Random;
class Test
{
public static void main (String[] args) throws
java.lang.Exception
{
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements to sort");
int n = in.nextInt();
int number_of_iterations = 5000;
sort(n,number_of_iterations);
}
public static void sort(int n,int number_of_iterations){
Random r = new Random();
int c, d, swap;
long running_time = 0,total_time = 0;
int i=0;
for(i=0;i<number_of_iterations;i++){
int array[] = new int[n];
for (c = 0; c < n; c++)
array[c] = r.nextInt();
System.out.println("Unsorted array :");
for (c = 0; c < n; c++)
System.out.println(array[c]);
long startTime = System.nanoTime();
bubble_sort(array);
long endTime = System.nanoTime();
System.out.println("Bubble Sorted array :");
for (c = 0; c < n; c++)
System.out.println(array[c]);
running_time = endTime - startTime;
total_time = total_time + running_time;
}
long total_time1 = total_time;
total_time = 0;
for (i=0;i<number_of_iterations;i++){
int array1[] = new int[n];
for (c = 0; c < n; c++)
array1[c] = r.nextInt();
System.out.println("Unsorted array :");
for (c = 0; c < n; c++)
System.out.println(array1[c]);
long startTime1 = System.nanoTime();
selection_sort(array1);
long endTime1 = System.nanoTime();
System.out.println("Selection Sorted array :");
running_time = endTime1 - startTime1;
total_time = total_time + running_time;
for (c = 0; c < n; c++)
System.out.println(array1[c]);
}
System.out.println("total time for Bubble Sort= "+total_time1+"
nanoseconds");
System.out.println("Average running time for each array in Bubble
Sort = "+(total_time1/number_of_iterations)+" nanoseconds");
System.out.println("total time for Selection Sort=
"+total_time+" nanoseconds");
System.out.println("Average running time for each array in
Selection Sort= "+(total_time/number_of_iterations)+"
nanoseconds");
}
public static void bubble_sort(int array[]){
int n = array.length;
int c=0,d=0,swap;
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) {
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
}
public static void selection_sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing...
The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees 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, C++, C#, or whatever language you...
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 different 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 (Make sure...
Be sure to include the number of total swaps in each sort. Create a total_Time variable is the total time it takes all 1000 iterations to run. DO NOT INCLUE THE TIME IT TAKES TO GENERATE A UNIQUE RANDOM ARRAY EACH LOOP. In java, 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 different arrays of this size, timing the run to get...
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, 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....
Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...
Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...
Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...