GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Programs/WiggleSorting.java at main · arshcoder13/Java Expand file tree/ Copy pathWiggleSorting.javaMore file actions 56 lines (48 loc) · 1.67 KB / Copy pathWiggleSorting.javaFile metadata and controls56 lines (48 loc) · 1.67 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 package sorting; import java.util.*; class SortWave { // A utility method to swap two numbers. void swap(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } // This function sorts arr[0..n-1] in wave form, i.e., // arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4].... void sortInWave(int[] arr, int n) { // Traverse all even elements for (int i = 0; i < n; i += 2) { // If current even element is smaller // than previous if (i > 0 && arr[i - 1] > arr[i]) swap(arr, i - 1, i); // If current even element is smaller // than next if (i < n - 1 && arr[i] < arr[i + 1]) swap(arr, i, i + 1); } } } public class WiggleSorting { public static void main(String[] args) { // What is wiggle sort? // 1.To sort an array in waveform. // 2.The array’s elements shall be ordered such that, // arr[0]<arr[1]>arr[2]<arr[3] and so on in wiggle sort.(Even index have smaller value and odd // index having larger values) //Time complexity : average O(n) time . Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements of required array :"); int n= sc.nextInt(); int[] arr= new int[n]; System.out.println("Enter the array(enter only positive integer):"); for(int i =0; i<n; i++){ arr[i]=sc.nextInt(); } SortWave ob = new SortWave(); ob.sortInWave(arr, n); for (int i : arr) { System.out.print(i+" "); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |