Skip to content

Commit 78dbc99

Browse files
committed
New Problem
New Problem
1 parent 3a0c06b commit 78dbc99

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*Copyright (c) Jul 3, 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 : ConvertArraytoSawToothWaveLinearTime.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 ConvertArraytoSawToothWaveLinearTime {
20+
public void convertArraytoSawToothWaveLinearTime(int A[]){
21+
// Traverse all even elements
22+
for (int i = 0; i < A.length; i+=2){
23+
// If current even element is smaller than previous
24+
if (i>0 && A[i-1] > A[i] )
25+
swap(A, i, i-1);
26+
// If current even element is smaller than next
27+
if (i<A.length-1 && A[i] < A[i+1] )
28+
swap(A, i, i+1);
29+
}
30+
}
31+
private void swap(int A[],int low,int high){
32+
int temp = A[low];
33+
A[low] = A[high];
34+
A[high] = temp;
35+
}
36+
37+
public static void main(String args[]){
38+
ConvertArraytoSawToothWaveLinearTime convertedArray = new ConvertArraytoSawToothWaveLinearTime();
39+
int A[] = {0,-6,9,13,10,-1,8,12,54,14,-5};
40+
for(int i=0; i < A.length; i++){
41+
System.out.print(A[i] + " ");
42+
}
43+
System.out.println();
44+
convertedArray.convertArraytoSawToothWaveLinearTime(A);
45+
for(int i=0; i < A.length; i++){
46+
System.out.print(A[i] + " ");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)