-
Notifications
You must be signed in to change notification settings - Fork 20.8k
Tim sort Algorithm | #2003 #2110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ff3e8f0
Add Tim Sort implementation in Java
hemanth-kotagiri 8e60fb1
Merge branch 'master' of https://github.com/TheAlgorithms/Java into t…
hemanth-kotagiri 120aee8
Add comments and complete implementation of TimSort Algorithm
hemanth-kotagiri e974ddf
Add better docs
hemanth-kotagiri edcd7e9
Merge branch 'master' of https://github.com/TheAlgorithms/Java into t…
hemanth-kotagiri e9458db
Add @brief's and test method
hemanth-kotagiri 75b4a8e
Fix errors
hemanth-kotagiri 0a86837
add Test method
hemanth-kotagiri 327aea0
Update Sorts/TimSort.java
hemanth-kotagiri e673418
Update Sorts/TimSort.java
hemanth-kotagiri f09cd0d
Update Sorts/TimSort.java
hemanth-kotagiri 40dc8f8
Update Sorts/TimSort.java
hemanth-kotagiri 324cb4e
Update Sorts/TimSort.java
hemanth-kotagiri e29cb48
Update Sorts/TimSort.java
hemanth-kotagiri dd8ad4b
Update Sorts/TimSort.java
hemanth-kotagiri d1660bf
Update Sorts/TimSort.java
hemanth-kotagiri 51e5627
Add tests
hemanth-kotagiri 0223914
Merge branch 'tim_sort' of https://github.com/hemanth-kotagiri/Java i…
hemanth-kotagiri ed665f1
Update Sorts/TimSort.java
hemanth-kotagiri 179c84a
Update Sorts/TimSort.java
ayaankhan98 0e81e1d
Update Sorts/TimSort.java
hemanth-kotagiri d76e46d
Update Sorts/TimSort.java
hemanth-kotagiri 4d7862f
Update Sorts/TimSort.java
hemanth-kotagiri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| 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; | ||
| int RUN = 32; | ||
|
|
||
| /** | ||
| * @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. | ||
| * @param array_length length given by the user. | ||
| */ | ||
|
|
||
| public TimSort(int array_length) { | ||
| Random rand = new Random(); | ||
|
|
||
| this.array_length = array_length; | ||
| this.array = new int[this.array_length]; | ||
|
|
||
| for (int i = 0; i < this.array_length; i++) { | ||
| int random_number = rand.nextInt(1000); | ||
| this.array[i] = random_number; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @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. | ||
| */ | ||
|
|
||
| public TimSort() { | ||
| this.array_length = 100; | ||
| this.array = new int[this.array_length]; | ||
|
|
||
| Random rand = new Random(); | ||
| for (int i = 0; i < this.array_length; i++) { | ||
| int random_number = rand.nextInt(1000); | ||
| this.array[i] = random_number; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @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. | ||
| */ | ||
|
|
||
| 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]; | ||
| int j = i - 1; | ||
| while (j >= 0 && array[j] > current_element) { | ||
| array[j + 1] = array[j]; | ||
| j--; | ||
| } | ||
| array[j + 1] = current_element; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief A method to merge two runs(chunks of array). | ||
| * @param array: The origin array which is to be sorted. | ||
| * @param start: Starting 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). | ||
| */ | ||
|
|
||
| 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 array1[] = new int[first_array_size], array2[] = new int[second_array_size]; | ||
| 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]; | ||
|
|
||
| i = 0; | ||
| j = 0; | ||
| k = start; | ||
|
|
||
| while (i < first_array_size && j < second_array_size) { | ||
| if (array1[i] <= array2[j]) { | ||
| array[k] = array1[i]; | ||
| i++; | ||
| } else { | ||
| array[k] = array2[j]; | ||
| j++; | ||
| } | ||
| k++; | ||
| } | ||
|
|
||
| while (i < first_array_size) { | ||
| array[k] = array1[i]; | ||
| k++; | ||
| i++; | ||
| } | ||
|
|
||
| while (j < second_array_size) { | ||
| array[k] = array2[j]; | ||
| k++; | ||
| j++; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Tim Sort Algorithm method. | ||
| */ | ||
|
|
||
| public void algorithm() { | ||
| // Before Sorting | ||
| System.out.println("Before sorting the array: "); | ||
| this.showArrayElements(); | ||
| System.out.println(); | ||
|
|
||
| // Applying insertion sort on RUNS. | ||
| for (int i = 0; i < this.array_length; i += this.RUN) | ||
| this.insertion_sort(this.array, i, Math.min(i + this.RUN, (this.array_length - 1))); | ||
|
|
||
| for (int split = this.RUN; split < this.array_length; split = 2 * split) { | ||
| for (int start_idx = 0; start_idx < this.array_length; start_idx += 2 * split) { | ||
| int mid = start_idx + split - 1; | ||
| int end_idx = Math.min((start_idx + 2 * split - 1), (this.array_length - 1)); | ||
|
|
||
| this.merge_runs(this.array, start_idx, mid, end_idx); | ||
| } | ||
| } | ||
| // After sorting | ||
| System.out.println("After sorting the array: "); | ||
| this.showArrayElements(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| /** | ||
| * @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] + " "); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief A method to test the sorting algorithm | ||
| */ | ||
|
|
||
| static void test() { | ||
| 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"; | ||
| } | ||
|
|
||
| // 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"; | ||
| } | ||
|
|
||
| // 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"; | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| test(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.