Skip to content

Commit 9124543

Browse files
committed
New Problem
New Problem
1 parent 35df27f commit 9124543

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*Copyright (c) Jul 2, 2015 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : ConvertArraytoSawToothWave.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter10sorting;
16+
17+
import java.util.Arrays;
18+
19+
public class ConvertArraytoSawToothWave {
20+
21+
/**
22+
* Sort the array first.
23+
* Then swap every adjacent element to get final result
24+
* @param A
25+
*/
26+
public void converttoSawToothWave(int A[]){
27+
for(int i=1; i < A.length; i+=2){
28+
if(i+1 < A.length){
29+
swap(A, i, i+1);
30+
}
31+
}
32+
}
33+
private void swap(int A[],int low,int high){
34+
int temp = A[low];
35+
A[low] = A[high];
36+
A[high] = temp;
37+
}
38+
39+
public static void main(String args[]){
40+
ConvertArraytoSawToothWave convertedArray = new ConvertArraytoSawToothWave();
41+
int A[] = {0,-6,9,13,10,-1,8,12,54,14,-5};
42+
Arrays.sort(A);
43+
for(int i=0; i < A.length; i++){
44+
System.out.print(A[i] + " ");
45+
}
46+
System.out.println();
47+
48+
convertedArray.converttoSawToothWave(A);
49+
for(int i=0; i < A.length; i++){
50+
System.out.print(A[i] + " ");
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)