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