import java.util.Random;
public class SortingsDemo {
static int quickSortComaprsions = 0;
static int bubbleSortComaprison = 0;
static int insertionSortComparsion = 0;
public static void main(String[] args) {
runSort(10);
runSort(100);
runSort(1000);
}
static void runSort(int no) {
for (int i=0;i<no;i++) {
int arr[] = new int[no];
int arr2[] = new int[no];
int arr3[] = new int[no];
System.out.println("----------------------------------------------------------------------------------------");
fillArry(arr,no);
fillArry(arr2,no);
fillArry(arr3,no);
bubbleSort(arr);
insertionSort(arr2);
intializeQuickSort(arr3);
}
}
private static void intializeQuickSort(int[] arr) {
System.out.println();
System.out.println("quickSort sort");
System.out.println("Original list::");
quickSortComaprsions = 0;
quicksort(arr,0,arr.length-1);
printArray(arr);
System.out.println("No of comparsions::"+quickSortComaprsions);
System.out.println("Sorted list");
printArray(arr);
System.out.println();
}
static void fillArry(int arr[], int no) {
Random r = new Random();
for (int i = 0; i < no; i++) {
arr[i] = r.nextInt(99 + 1);
}
}
static void printArray(int arr[]) {
int length = arr.length;
for (int index = 0; index < length; ++index)
System.out.print(arr[index] + " ");
System.out.println();
}
static void bubbleSort(int arr[]) {
System.out.println();
System.out.println("Bubble sort");
System.out.println("Original list::");
bubbleSortComaprison = 0;
printArray(arr);
int length = arr.length;
for (int index = 1; index < length; ++index) {
int key = arr[index];
int innerIndex = index - 1;
while (innerIndex >= 0 && arr[innerIndex] > key) {
bubbleSortComaprison++;
arr[innerIndex + 1] = arr[innerIndex];
innerIndex = innerIndex - 1;
}
arr[innerIndex + 1] = key;
}
System.out.println("No Of Comparsions::" + bubbleSortComaprison);
System.out.println("Sorted List");
printArray(arr);
System.out.println();
}
static void insertionSort(int arr[]) {
System.out.println();
System.out.println("insertionSort sort");
System.out.println("Original list::");
printArray(arr);
insertionSortComparsion = 0;
int length = arr.length;
for (int index = 1; index < length; ++index) {
int key = arr[index];
int keyCompareIndex = index - 1;
while (keyCompareIndex >= 0 && arr[keyCompareIndex] > key) {
insertionSortComparsion++;
arr[keyCompareIndex + 1] = arr[keyCompareIndex];
keyCompareIndex = keyCompareIndex - 1;
}
arr[keyCompareIndex + 1] = key;
}
System.out.println("No Of Comparsions::" + insertionSortComparsion);
System.out.println("Sorted List");
printArray(arr);
System.out.println();
}
static int partition(int arr[], int low, int high) {
int pivot = arr[high];
int index = (low - 1);
for (int innerIndex = low; innerIndex < high; innerIndex++) {
if (arr[innerIndex] <= pivot) {
quickSortComaprsions++;
index++;
int temp = arr[index];
arr[index] = arr[innerIndex];
arr[innerIndex] = temp;
}
}
int temp = arr[index + 1];
arr[index + 1] = arr[high];
arr[high] = temp;
return index + 1;
}
static void quicksort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
}
Java We will look at the behavior of three different sorts on randomly generated lists of...
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...
Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...
2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.
Write a complete C++ program that analyzes the following sorts by keeping track of how long they take to sort arrays of longS of size 5000, 10000, and 100000, as well as the number of swaps and comparisons they take in doing so: Insertion sort Selection sort Quick sort shell sort(using gaps of n/2, n/4, n/8, ..., 1) for 20, the gaps would be 10, 5, 2, and 1 shell sort(using gaps based on 2^k + 1, but starting at...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
**C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...
Practical 5: Write a program that implements several sorting
algorithms, and use it to demonstrate the comparative performance
of the algorithms for a variety of data sets.
Need Help With this Sorting Algorithm task for C++
Base Code for sorting.cpp is given.
The header file is not included in this. Help would be much
appreciated as I have not started on this due to personal
reasons
#include <cstdlib>
#include <iostream>
#include <getopt.h>
using namespace std;
long compares; // for counting...
Can I get some help with this question for c++ if you can add
some comments too to help understand that will be much
appreciated.
Code:
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <string>
using namespace std;
static long comparisons = 0;
static long swaps = 0;
void swap(int *a, int *b)
{
// add code here
}
void selectionSort(int *first, int *last)
{
// add code here
}
void insertionSort(int *first, int *last)
{
// add code here
}...