HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Sorting Algorithms

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingalgorithmsstackoverflow

Problem

A group of basic sorting algos. Based on Algorithms, 4th Edition - Robert Sedgewick | Kevine Wayne. Just making sure all of my logic and everything is correct.

```
/*
Sandbox for the various search algorithms from Section 2 of
Algorithms, 4th Edition - Robert Sedgewick | Kevine Wayne
*/

import java.util.Arrays;

public class Sorts {

/
Sorting Algorithms
/

/*Selection Sort

Sorts a passed array of any Comparable object by ascending order.Uses the Selection Sort method. For each iteration i we place the ith smallest item in array[i]*/
public static void selectionSort(Comparable[] toSort) {
int N = toSort.length;
int min; //Index of the minimal element during each run

for (int i=0; i start && less(toSort[j], toSort[j-1]); j--) {
swap(toSort, j, j-1);
}
}
}

/*Shell Sort

Sorts a passed array of any Comparable object by ascending order. Uses the Shell Sort method, which is essentially a modified Insertion Short. Rather then decrementing by 1, we decrement by decreasing values of h, breaking the array into smaller and smaller already sorted sub-arrays. Increased performance on larger arrays, especially when there are very small values at the end of the array*/
public static void shellSort(Comparable[] toSort) {
int N = toSort.length;
int h = 1;

while (h = 1) {
for (int i = h; i = h && less(toSort[j], toSort[j-h]); j-=h) {
swap(toSort, j, j-h);
}
}
h = h/3; //Shrinks to the next h-array size
}
}

/*Merge Sort

Sorts a passed array of any Comparable object by ascending order. Uses the Merge Sort method. Recursively breaks the array into 1/2 sized sub arrays, then merges them in sorted order as the stack unwinds.

Uses Insertion sort when it gets to a certain threshold for small arrays*/
pub

Solution

Good job in general. Just few comments.

-
All the algorithms are implemented on arrays. It is really an overkill; you don't need a random access to sort a collection.

-
I am not sure you need to spell out less, equals or greater. Isn't it what Comparable is for?

-
I am not sure that isSorted(Comparable[] a, int lo, int hi) needs to exist at all. It doesn't hurt to have it though.

-
mergeArrays()

A naked loop usually represents an important algorithm. In this case a for (int k = low; k mid condition is encountered, there is no point to test it over and over again: break the main loop right away and let a copy() method do the rest. Same goes for j > high.

-
quickPartition()

Unnecessary comments sometimes create very hard to understand problems. It took me a while to realize that greater at line 153 is an unfortunate copy-paste. Yet another reason to convert a naked loop into a method.

-
insertionSort()

Same naked loop problem. An inner loop is identical to one of the quickPartition's loops.

-
selectionSort()

See an insertionSort comment.

-
shellSort()

Did I mention naked loops?

-
main()

Do something with all those Stopwatches! You already provided arrayCopy - why not use it? The Java experts will comment on Java specifics (final especially).

Context

StackExchange Code Review Q#59966, answer score: 7

Revisions (0)

No revisions yet.