Skip to content
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

feat: Add Implementation for SHIT Algorithm #213

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,38 @@ public class LinearSearchIn2dArraySnippet {
}
```

### Linear Search
### SortingHeuristicIntelligentTable

```java
public static void SortingHeuristicIntelligentTable(int[] arr) {
ArrayList<Integer>[] buckets = new ArrayList[10];

for (int i = 0; i < 10; i++) {
buckets[i] = new ArrayList<>();
}

// Distribute elements into buckets based on the tens place
for (int num : arr) {
int tensPlace = num / 10;
buckets[tensPlace].add(num);
}

// Sort each bucket
for (ArrayList<Integer> bucket : buckets) {
Collections.sort(bucket);
}

// Merge the sorted buckets back into the original array
int index = 0;
for (ArrayList<Integer> bucket : buckets) {
for (int num : bucket) {
arr[index++] = num;
}
}
}
```

### LinearSearch

```java
public class LinearSearchSnippet {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppäläy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

import java.util.ArrayList;
import java.util.Collections;

/**
* SortingHeuristicIntelligentTableSnippet.
*/
public class SortingHeuristicIntelligentTableSnippet {
/**
* Sort an array with SHIT algorithm
* For details around implementation, please refer - i
*
* @param arr array to sort
*/
public static void sortHeuristicIntelligentTable(int[] arr) {

//Create 10 buckets from 0-9 to
ArrayList<Integer>[] buckets = new ArrayList[10];

for (int i = 0; i < 10; i++) {
buckets[i] = new ArrayList<>();
}

// Distribute elements into buckets based on the tens place
for (int num : arr) {
int tensPlace = num / 10;
buckets[tensPlace].add(num);
}

// Sort each bucket
for (ArrayList<Integer> bucket : buckets) {
Collections.sort(bucket);
}

// Merge the sorted buckets back into the original array
int index = 0;
for (ArrayList<Integer> bucket : buckets) {
for (int num : bucket) {
arr[index++] = num;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/*
* Tests for 30 Seconds of Java code library
*
*/
public class SortingHeuristicIntelligentTableSnippetTest {
/**
* Tests for {@link SortingHeuristicIntelligentTableSnippet (int)}.
*/
@Test
void testSortingHeuristicIntelligentTable() {
var arr = new int[] {7, 13, 3, 1, 8, 5};
SortingHeuristicIntelligentTableSnippet.sortHeuristicIntelligentTable(arr);
assertEquals(1, arr[0]);
assertEquals(3, arr[1]);
assertEquals(5, arr[2]);
assertEquals(7, arr[3]);
assertEquals(8, arr[4]);
assertEquals(13, arr[5]);
}
}
Loading