diff --git a/Java/BucketSort.java b/Java/BucketSort.java new file mode 100644 index 0000000..bdf7c09 --- /dev/null +++ b/Java/BucketSort.java @@ -0,0 +1,63 @@ +import java.util.*; + +/* +1) Create n empty buckets (Or lists). +2) Do following for every array element arr[i]. +.......a) Insert arr[i] into bucket[n*array[i]] +3) Sort individual buckets using insertion sort. +4) Concatenate all sorted buckets. +*/ + +class BucketSort { + public static int[] bucketSort(int[] array, int bucketCount) { + + if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count"); + if (array.length <= 1) return array; //trivially sorted + int high = array[0]; + int low = array[0]; + for (int i = 1; i < array.length; i++) { //find the range of input elements + if (array[i] > high) high = array[i]; + if (array[i] < low) low = array[i]; + } + double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket + ArrayList buckets[] = new ArrayList[bucketCount]; + for (int i = 0; i < bucketCount; i++) { //initialize buckets + buckets[i] = new ArrayList(); + } + for(int i = 0; i < array.length; i++) + { //partition the input array + buckets[(int)((array[i] - low)/interval)].add(array[i]); + } + int pointer = 0; + for (int i = 0; i < buckets.length; i++) + { + Collections.sort(buckets[i]); //mergeSort + for (int j = 0; j < buckets[i].size(); j++) + { //merge the buckets + array[pointer] = buckets[i].get(j); + pointer++; + } + + } + return array; + + } + + + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); + int[] a=new int[t]; + for(int i=0;i max) + max = arr[i]; + if (arr[i] < min) + min = arr[i]; + } + int range = max - min + 1; + + /** check if range is small enough for count array **/ + /** else it might give out of memory exception while allocating memory for array **/ + if (range > MAX_RANGE) + { + System.out.println("\nError : Range too large for sort"); + return; + } + + int[] count = new int[range]; + /** make count/frequency array for each element **/ + for (int i = 0; i < N; i++) + count[arr[i] - min]++; + /** modify count so that positions in final array is obtained **/ + for (int i = 1; i < range; i++) + count[i] += count[i - 1]; + /** modify original array **/ + int j = 0; + for (int i = 0; i < range; i++) + while (j < count[i]) + arr[j++] = i + min; + } + /** Main method **/ + public static void main(String[] args) + { + Scanner scan = new Scanner( System.in ); + System.out.println("Counting Sort Test\n"); + int n, i; + /** Accept number of elements **/ + System.out.println("Enter number of integer elements"); + n = scan.nextInt(); + /** Create integer array on n elements **/ + int arr[] = new int[ n ]; + /** Accept elements **/ + System.out.println("\nEnter "+ n +" integer elements"); + for (i = 0; i < n; i++) + arr[i] = scan.nextInt(); + /** Call method sort **/ + sort(arr); + /** Print sorted Array **/ + System.out.println("\nElements after sorting "); + for (i = 0; i < n; i++) + System.out.print(arr[i]+" "); + System.out.println(); + } +} + + + +// Counting Sort Test + +// Enter number of integer elements +// 20 + +// Enter 20 integer elements +// 54 67 13 24 76 37 97 10 67 24 6 28 5 19 63 1 71 83 97 24 + +// Elements after sorting +// 1 5 6 10 13 19 24 24 24 28 37 54 63 67 67 71 76 83 97 97 \ No newline at end of file diff --git a/Java/CycleSort.java b/Java/CycleSort.java new file mode 100644 index 0000000..e3c79c3 --- /dev/null +++ b/Java/CycleSort.java @@ -0,0 +1,58 @@ +import java.util.*; +public class CycleSort +{ +static void sort(int a[], int n) +{ + int writes = 0,start,element,pos,temp,i; + + for (start = 0; start <= n - 2; start++) { + element = a[start]; + pos = start; + for (i = start + 1; i < n; i++) + if (a[i] < element) + pos++; + if (pos == start) + continue; + while (element == a[pos]) + pos += 1; + if (pos != start) { + //swap(element, a[pos]); + temp = element; + element = a[pos]; + a[pos] = temp; + writes++; + } + while (pos != start) { + pos = start; + for (i = start + 1; i < n; i++) + if (a[i] < element) + pos += 1; + while (element == a[pos]) + pos += 1; + if (element != a[pos]) { + temp = element; + element = a[pos]; + a[pos] = temp; + writes++; + } + } + } +} +public static void main(String[] args) +{ + Scanner sc=new Scanner(System.in); + System.out.println("Enter number of elements: "); + int n1=sc.nextInt(); + int[] a=new int[n1]; + for(int i=0;i