Skip to content

Commit 075d88e

Browse files
Merge pull request #221 from Anova07/patch-1
Create SortedSquares.java
2 parents ef982ed + 95a9783 commit 075d88e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

LeetCode/SortedSquares.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
// Java program to Sort the square of the numbers
3+
// of the array
4+
import java.util.*;
5+
import java.io.*;
6+
public class sortsq
7+
{
8+
// Function to sort an square array
9+
public static void sortSquares(int arr[])
10+
{
11+
int n = arr.length;
12+
// First convert each array elements
13+
// into its square
14+
for (int i = 0 ; i < n ; i++)
15+
arr[i] = arr[i] * arr[i];
16+
17+
// using "inbuild sort function" for sorting
18+
// in Arrays class.
19+
Arrays.sort(arr);
20+
}
21+
//sample driver program
22+
public static void main (String[] args)
23+
{
24+
int arr[] = { -17 , -12 , -1 , 4 , 8 , 15 };
25+
int n = arr.length;
26+
27+
System.out.println("Original ");
28+
for (int i = 0; i < n; i++)
29+
System.out.print(arr[i] + " ");
30+
31+
sortSquares(arr);
32+
System.out.println("");
33+
System.out.println("Implementing sort");
34+
for (int i = 0 ; i < n ; i++)
35+
System.out.print(arr[i] + " ");
36+
37+
}
38+
}

0 commit comments

Comments
 (0)