- (구글링했음)
import java.util.*;
public class pivot {
public static void main(String[] args) {
System.out.print("Enter the number of elements: ");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
System.out.print("Enter a list: ");
int[] arr=new int[num];
for(int i=0; i<arr.length; i++) {
arr[i]=input.nextInt();
}
partition(arr);
System.out.print("After the partition, the list is ");
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static int partition(int[] list) {
int pivot=list[0];
int i=0;
int j=list.length-1;
while(i<j) {
while(pivot<list[j]) {
j--;
}
while(i<j && pivot>=list[i]) {
i++;
}
int tmp=list[i];
list[i]=list[j];
list[j]=tmp;
}
list[0]=list[i];
list[i]=pivot;
return i;
}
}
2.(구글링 했음)
import java.util.*;
public class workhour {
public static void main(String[] args) {
System.out.println("근로시간을 입력하시오.\n");
int[][] employee=new int[8][7];
Scanner input=new Scanner(System.in);
for(int row=0;row<employee.length;row++) { //이차원 배열을 입력받음
for(int column=0;column<employee[row].length;column++) {
employee[row][column]=input.nextInt();
}
}
int[] workHour=new int[8]; //행 별로 근로시간을 더함
for(int i=0; i<8; i++) {
for(int column=0;column<7;column++) {
workHour[i]+=employee[i][column];
}
}
int[][] value=new int[8][2]; //근로시간과 근로자를 연결
for(int i=0; i<8; i++) {
value[i][0]=i;
value[i][1]=workHour[i];
}
int tmp=0;
int temp=0; //배열 value를 내림차순으로 정렬함
for(int i=0; i<8; i++) {
for(int j=i+1; j<8; j++) {
if(value[i][1]<value[j][1]) {
temp = value[i][1];
tmp=value[i][0];
value[i][1]=value[j][1];
value[i][0]=value[j][0];
value[j][1]=temp;
value[j][0]=tmp;
}
}
}
for(int i=0; i<8; i++) {
System.out.println("Employee "+value[i][0]+": "+value[i][1]);
}
}
}