From 3e3878f950350493d3ecae80ba49aead1f058f47 Mon Sep 17 00:00:00 2001 From: LOCODAK <108181817+LOCODAK@users.noreply.github.com> Date: Fri, 28 Oct 2022 21:52:03 +0530 Subject: [PATCH] Add files via upload Stooge sort --- stooge_sort.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 stooge_sort.java diff --git a/stooge_sort.java b/stooge_sort.java new file mode 100644 index 000000000..00ef95140 --- /dev/null +++ b/stooge_sort.java @@ -0,0 +1,41 @@ +public class stooge_sort { + static void stoogesort(int arr[], int l, int h) { + if (l >= h) + return; + + // If first element is smaller + // than last, swap them + if (arr[l] > arr[h]) { + int t = arr[l]; + arr[l] = arr[h]; + arr[h] = t; + } + + // If there are more than 2 elements in + // the array + if (h - l + 1 > 2) { + int t = (h - l + 1) / 3; + + // Recursively sort first 2/3 elements + stoogesort(arr, l, h - t); + + // Recursively sort last 2/3 elements + stoogesort(arr, l + t, h); + + // Recursively sort first 2/3 elements + // again to confirm + stoogesort(arr, l, h - t); + } + } + + // Driver Code + public static void main(String args[]) { + int arr[] = { 2, 4, 5, 3, 1 }; + int n = arr.length; + + stoogesort(arr, 0, n - 1); + + for (int i = 0; i < n; i++) + System.out.print(arr[i] + " "); + } +}