循环结构
循环语句:for语句、while语句、do-while语句 While(3>4)不能编译,while(a > b)能编译,因为a,b的值有可能会改变 已知循环次数用for,不知道循环次数用但知道判断条件时用while
直接排序
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52,-111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int i
= 0; i
< a
.length
; i
++){
for(int j
= i
; j
< a
.length
; j
++){
if(a
[i
] > a
[j
]){
swap(i
, j
, a
);
}
}
}
}
public static void swap(int i
, int j
, int[]a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ “”
);
}
}
}
输出结果为:
-111 -99 -45 -2 0 0 52 100
选择排序:
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52,-111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int i
= 0; i
< a
.length
; i
++){
int k
= i
;
for(int j
= i
; j
< a
.length
; j
++){
if(a
[k
] > a
[j
]){
k
= j
;
}
}
swap(i
, k
, a
);
}
}
public static void swap(int i
, int j
, int[]a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ “ ”
);
}
}
}
输出结果为:
-111 -99 -45 -2 0 0 52 100
冒泡排序:
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52, -111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int i
= 0; i
< a
.length
; i
++){
for(int j
= 0; j
< a
.length
- 1 - I
; j
++){
if(a
[j
] > a
[j
+ 1]){
swap(j
+ 1, j
, a
);
}
}
}
}
public static void swap(int i
, int j
, int[] a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ “”
);
}
}
}
输出结果为:
-111 -99 -45 -2 0 0 52 100
插入排序(1):
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52, -111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int i
= 1; i
< a
.length
; i
++){
for(int j
= i
; j
> 0; j
--){
if(a
[j
] > a
[j
- 1]){
swap(j
- 1, j
, a
);
}
}
}
}
public static void swap(int i
, int j
, int[] a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ " ");
}
}
}
插入排序(2):
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52, -111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int i
= 1; i
< a
.length
; i
++){
int temp
= a
[i
];
int j
= i
;
while(j
> 0 && temp
< a
[j
- 1]){
swap(j
- 1, j
, a
);
j
--;
}
a
[j
] = temp
;
}
}
public static void swap(int i
, int j
, int[] a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ " ");
}
}
}
输出结果为:
-111 -99 -45 -2 0 0 52 100
希尔排序:
public class Test{
public static void main(String
[] args
){
int[] a
= {-2, -99, -45, 100, 52, -111, 0, 0};
sort(a
);
print(a
);
}
public static void sort(int[] a
){
for(int b
= 4; b
> 0; b
/= 2){
for(int i
= b
; i
< a
.length
; i
++){
int temp
= a
[i
];
int j
= i
;
while(j
> b
- 1 && temp
< a
[j
- b
]){
swap(j
- b
, j
, a
);
j
-= b
;
}
a
[j
] = temp
;
}
}
}
public static void swap(int i
, int j
, int[] a
){
int temp
= a
[i
];
a
[i
] = a
[j
];
a
[j
] = temp
;
}
public static void print(int[] a
){
for(int e
: a
){
System
.out
.print(e
+ " ");
}
}
}
输出结果为:
-111 -99 -45 -2 0 0 52 100
转载请注明原文地址: https://mac.8miu.com/read-492819.html