-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionSortNegative.java
More file actions
40 lines (34 loc) · 922 Bytes
/
Copy pathSolutionSortNegative.java
File metadata and controls
40 lines (34 loc) · 922 Bytes
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
import java.io.*;
import java.util.*;
/*
*Q1. Sort an array without changing position of negative numbers
Given an array arr[] of N integers, the task is to sort the array without changing the position of negative numbers (if any) i.e. the negative numbers need not be sorted.
Examples:
Input: arr[] = {2, -6, -3, 8, 4, 1}
Output: 1 -6 -3 2 4 8
Input: arr[] = {-2, -6, -3, -8, 4, 1}
Output: -2 -6 -3 -8 1 4
*/
class SolutionSortNegative {
public static void main(String[] args) {
int[] arr = {2, -6, 0, -3, 8,1, 4, 1};
sort(arr);
display(arr);
}
public static void sort(int[] a){
for(int i=0;i<a.length-1;i++){
for(int j=i+1;j<a.length;j++){
if( a[i]>=0 && a[j]>=0 && a[i]>a[j]){
int temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
public static void display(int[] a){
for (int i =0;i<a.length;i++) {
System.out.println(a[i]+" ");
}
}
}