Skip to content

Commit c64a16b

Browse files
authored
Merge pull request #470 from Shubh1006/wiggle
addig wiggle sort
2 parents 4273241 + d93d6f8 commit c64a16b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

java/sorting/wiggle_sort.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import java.util.*;
3+
4+
class sortInwiggle
5+
{
6+
7+
void swap(int arr[], int a, int b)
8+
{
9+
int temp = arr[a];
10+
arr[a] = arr[b];
11+
arr[b] = temp;
12+
}
13+
14+
void sortInwiggle(int arr[], int n)
15+
{
16+
Arrays.sort(arr);
17+
for (int i=0; i<n-1; i += 2)
18+
swap(arr, i, i+1);
19+
}
20+
21+
public static void main(String args[])
22+
{
23+
SortWave ob = new sortInwiggle();
24+
int arr[] = {10, 90, 49, 2, 1, 5, 23};
25+
int n = arr.length;
26+
ob.sortInwiggle(arr, n);
27+
for (int i : arr)
28+
System.out.print(i + " ");
29+
}
30+
}

0 commit comments

Comments
 (0)