Formatted with Google Java Formatter

This commit is contained in:
github-actions
2021-04-23 17:28:12 +00:00
parent 66adafbff8
commit a8505097a8

View File

@ -1,13 +1,11 @@
package Sorts;
import java.lang.Math;
import java.util.Random;
/**
* @author [Hemanth Kotagiri](https://github.com/hemanth-kotagiri)
* @see [Tim Sort](https://en.wikipedia.org/wiki/Tim_sort)
*/
class TimSort {
int array[];
int array_length;
@ -17,18 +15,15 @@ class TimSort {
* @brief A constructor which takes in the array specified by the user.
* @param array : Array given by the user.
*/
public TimSort(int[] array) {
this.array = array;
this.array_length = array.length;
}
/**
* @brief A constructor which takes in an array length and randomly
* initializes an array.
* @brief A constructor which takes in an array length and randomly initializes an array.
* @param array_length length given by the user.
*/
public TimSort(int array_length) {
Random rand = new Random();
@ -45,17 +40,14 @@ class TimSort {
* @brief A method to change the size of the run.
* @param run : Value specified by the user to change the run.
*/
public void change_run(int run) {
this.RUN = run;
}
/**
* @brief A default constructor when no parameters are given.
* Initializes the array length to be 100.
* Generates a random number array of size 100.
* @brief A default constructor when no parameters are given. Initializes the array length to be
* 100. Generates a random number array of size 100.
*/
public TimSort() {
this.array_length = 100;
this.array = new int[this.array_length];
@ -68,15 +60,11 @@ class TimSort {
}
/**
* @brief Performs Insertion Sort Algorithm on given array with bounded
* indices.
* @brief Performs Insertion Sort Algorithm on given array with bounded indices.
* @param array: The array on which the algorithm is to be performed.
* @param start_idx: The starting index from which the algorithm is to be
* performed.
* @param end_idx: The ending index at which the algorithm needs to stop
* sorting.
* @param start_idx: The starting index from which the algorithm is to be performed.
* @param end_idx: The ending index at which the algorithm needs to stop sorting.
*/
public void insertion_sort(int[] array, int start_idx, int end_idx) {
for (int i = 0; i < array.length; i++) {
int current_element = array[i];
@ -96,7 +84,6 @@ class TimSort {
* @param mid: The ending index of the first run(chunk).
* @param end: Ending index of the second run(chunk).
*/
public void merge_runs(int array[], int start, int mid, int end) {
int first_array_size = mid - start + 1, second_array_size = end - mid;
@ -104,10 +91,8 @@ class TimSort {
int i = 0, j = 0, k = 0;
// Building the two sub arrays from the array to merge later
for (i = 0; i < first_array_size; i++)
array1[i] = array[start + i];
for (i = 0; i < second_array_size; i++)
array2[i] = array[mid + 1 + i];
for (i = 0; i < first_array_size; i++) array1[i] = array[start + i];
for (i = 0; i < second_array_size; i++) array2[i] = array[mid + 1 + i];
i = 0;
j = 0;
@ -137,10 +122,7 @@ class TimSort {
}
}
/**
* @brief Tim Sort Algorithm method.
*/
/** @brief Tim Sort Algorithm method. */
public void algorithm() {
// Before Sorting
System.out.println("Before sorting the array: ");
@ -165,10 +147,7 @@ class TimSort {
System.out.println();
}
/**
* @brief A method to show the elements inside the array.
*/
/** @brief A method to show the elements inside the array. */
public void showArrayElements() {
for (int i = 0; i < this.array.length; i++) {
System.out.print(this.array[i] + " ");
@ -176,34 +155,30 @@ class TimSort {
System.out.println();
}
/**
* @brief A method to test the sorting algorithm
*/
/** @brief A method to test the sorting algorithm */
static void test() {
int[] array = { 4, 1, 3, 17, 12, 11, 8 };
int[] array = {4, 1, 3, 17, 12, 11, 8};
TimSort sorterObj1 = new TimSort();
TimSort sorterObj2 = new TimSort(50);
TimSort sorterObj3 = new TimSort(array);
sorterObj1.algorithm();
sorterObj2.algorithm();
sorterObj3.algorithm();
// Testing the first array
for(int i = 0; i < sorterObj1.array_length - 1; i++) {
assert((sorterObj1.array[i] <= sorterObj1.array[i +1])) : "Array is not sorted";
for (int i = 0; i < sorterObj1.array_length - 1; i++) {
assert ((sorterObj1.array[i] <= sorterObj1.array[i + 1])) : "Array is not sorted";
}
// Testing the second array.
for(int i = 0; i < sorterObj2.array_length - 1; i++) {
assert((sorterObj2.array[i] <= sorterObj2.array[i + 1])) : "Array is not sorted";
for (int i = 0; i < sorterObj2.array_length - 1; i++) {
assert ((sorterObj2.array[i] <= sorterObj2.array[i + 1])) : "Array is not sorted";
}
// Testing the third array.
for(int i = 0; i < sorterObj3.array_length - 1; i++) {
assert((sorterObj3.array[i] <= sorterObj3.array[i + 1])) : "Array is not sorted";
for (int i = 0; i < sorterObj3.array_length - 1; i++) {
assert ((sorterObj3.array[i] <= sorterObj3.array[i + 1])) : "Array is not sorted";
}
}