0% found this document useful (0 votes)
60 views7 pages

To Implement Three Different Sorting Algorithms: Selection Sort, Quicksort and Bubblesort

This document implements and compares the performance of three sorting algorithms - selection sort, bubble sort, and quicksort - on arrays of different sizes containing sorted, randomly arranged, and partially sorted data. Based on the results, bubble sort performed best for fully sorted small arrays, while quicksort was most efficient for larger random arrays. The document recommends choosing an algorithm based on the array size and expected sortedness of the data.

Uploaded by

Torcoxk Namgay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views7 pages

To Implement Three Different Sorting Algorithms: Selection Sort, Quicksort and Bubblesort

This document implements and compares the performance of three sorting algorithms - selection sort, bubble sort, and quicksort - on arrays of different sizes containing sorted, randomly arranged, and partially sorted data. Based on the results, bubble sort performed best for fully sorted small arrays, while quicksort was most efficient for larger random arrays. The document recommends choosing an algorithm based on the array size and expected sortedness of the data.

Uploaded by

Torcoxk Namgay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Aim: To implement three different sorting algorithms: selection sort, quicksort and bubblesort

Program Code:

public class Lab6 {

/** Sorting algorithms

* @param data **/

//Sorting using Bubble Sort

public static void bubbleSort (int[] data){

int position, scan;

for (position = data.length - 1; position >= 0; position--){

for (scan = 0; scan <= position - 1; scan++){

if (data[scan] > data[scan+1]){

// Swap the values

swap(data,scan,scan+1);

// Selection sort.

public static void selectionSort(int[] data) {

int min;

for(int index = 0; index <= data.length-1; index++)

min = index;

for(int scan = index+1; scan < data.length; scan++){

if(data[scan] < data[min]){

min = scan;

}
}

swap(data,min,index);

// Quicksort.

public static void quickSort(int[] array) {

quickSort(array,0,array.length-1);

// Quicksort part of an array

private static void quickSort(int[] array, int begin, int end) {

if (begin < end)

/* pi is partitioning index, arr[p] is now at right place */

int pi = partition(array, begin, end);

// Separately sort elements before partition and after partition

quickSort(array, begin, pi - 1);

quickSort(array, pi + 1, end);

// Partition part of an array, and return the index where the pivot

// ended up.

private static int partition(int[] array, int begin, int end) {

int pivot = array[end]; // pivot

int i = (begin - 1); // Index of smaller element


for (int j = begin; j <= end- 1; j++){

// If current element is smaller than or equal to pivot

if (array[j] <= pivot){

i++; // increment index of smaller element

swap(array, i, j);

swap(array,i + 1, end);

return (i + 1);

// Swap two elements in an array

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

int temp = array[i];

array[i] = array[j];

array[j] = temp;

public void printArr(int []array){

for(int i=0;i < array.length;i++){

System.out.print(array[i]+" ");

System.out.println();

}
Bench.java Output:
Performance Recommendations:

The following can be observed from the benchmark results:

1. For fully sorted data, Bubble Sort performed significantly better than Quicksort and Selection
Sort in all sizes of arrays.
2. For 95% sorted and randomly arranged data, Bubble Sort is much faster than the other two for
smaller data. But Quicksort performed significantly better than the other two for larger array
sizes (100 and greater).

Based on these observations, I recommend Bubble Sort for small data sets and Quicksort for larger data
sets given that the data is random and not already sorted (in which case we wouldn’t require a sorting
algorithm in the first place).

To answer the questions:

1. Is there size cutoff at which different algorithm becomes the best?


 Yes, there are size cutoffs at which different algorithm becomes the best. For example, for
random and 95% sorted arrays, bubble sort was much faster up to array of length 30, after
which quick sort became much faster.

2. How does the type of test data affect which algorithm you should choose?
 For fully sorted data, bubble sort would be a better choice over quick sort and selection sort.
 For random data, quick sort would be the best choice.

3. Which should you choose if you don’t know anything about the test data?
 If the type and size of the test data is unknown, I would choose quick sort since it has the best
time complexity out of the three.

4. Are there circumstances when you should definitely avoid a certain algorithm?
 Yes, for circumstances where the data is fully sorted and of large size (over 3000 as per test
results), quick sort must be definitely avoided.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy