Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43a8ac4

Browse files
【added】快速排序,希尔排序,堆排序
1 parent 5fe1528 commit 43a8ac4

File tree

4 files changed

+384
-0
lines changed

4 files changed

+384
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package selectSort;
2+
3+
4+
//��Ҫ�����һ�����У�ѡ����С��������󣩵�һ�������1��λ�õ���������
5+
//Ȼ����ʣ�μ�������������С��������󣩵����2��λ�õ���������
6+
//�������ƣ�ֱ����n-1��Ԫ�أ������ڶ��������͵�n��Ԫ�أ����һ�������Ƚ�Ϊֹ��
7+
8+
public class UpdatedSelectSort {
9+
static final int SIZE=10;
10+
public static void select(int[]arr){
11+
// �����Ϸ��Լ��
12+
if (arr == null || arr.length <= 1) {
13+
return;
14+
}
15+
int minIndex,temp,maxIndex; //minIndexΪ��Сֵ������tempΪ����ֵ���м�ֵ,maxIndexΪ���ֵ������
16+
//����㷨��������ֻ����n/2�Σ�������
17+
for(int i=0;i<(arr.length)/2;i++){
18+
minIndex=i; //�Ƚ���ǰ�������±ꡰ�وٴ��塱Ϊ��Сֵ����
19+
maxIndex =i; //�Ƚ�max��min��ָ�������ĵ�һ��Ԫ��
20+
21+
for(int j=i;j<(arr.length) - i;j++){ //arr.lengh-i�����ǣ�ÿ������һ����С�������󣬶���̶�������߻������ұߵ�λ�ã���λ�ò����ٱ����˵ģ�����Ϊ�Ѿ�ȷ����������С!!!
22+
if(arr[j]<arr[minIndex]){ //����ѭ����ɦ����ұ��Ǹ���Сֵ����������������ҪС��ֵ
23+
minIndex=j; //����ҵ��ͽ�����ֵ��minIndex
24+
continue; //�ҵ��˾���ֹ����ѭ������Ϊjô�أ�����Ϊ��������������ѭ��Ϊ������ѭ���ģ���������ߵL·�ʼѭ�����ҵ���С�ľ͹̶���������ߣ��ڶ��־�����������ҿ�һλ��λ�ÿ�ʼѭ���ģ��ٴΰ�����Ǹ���С���ָ��ڶ���λ�ã�����
25+
}
26+
if(arr[j]>arr[maxIndex]){ //������ÿ�α���ѡ������ֵ�������±����ݽ��н�����Ȼ���ȷ�������ֵ�����ұߡ�Ȼ��ڶ��ִ����ұ�����߿�һλ���Ƕ����ݿ�ʼ���б����������ֵ��Ȼ��ŵ����ұ�����߿�һλ��λ�á�
27+
maxIndex=j;
28+
}
29+
}
30+
//ÿ��Ѱ������Լ���С�������ֱ����ʣ��λ�õ������Լ����ұ�
31+
//���ɦ����Сֵ���ڵ�i��
32+
// �����ֵ���ڵ�n-i-1��
33+
int maxtmp = 0;
34+
int mintmp = 0; //ע��:���عم�ܰ�a[maxIndex],a[minIndex]ֱ�Ӻ�a[i]��a[n-i-1]��������Ϊ��������ֱ�ӽ����Ļ�����ζ�Ű�ԭ��a[maxIndex]��a[minIndex]�������x���Ҫ������ֵ�l�
35+
maxtmp = arr[maxIndex]; //max�Ľ���
36+
mintmp = arr[minIndex];//min�Ľ���
37+
arr[minIndex] = arr[i];//min�Ľ���
38+
arr[maxIndex] = arr[arr.length-i-1];//max�Ľ���
39+
arr[i] = mintmp;//min�Ľ���
40+
arr[arr.length-i-1] = maxtmp;//max�Ľ���
41+
42+
43+
System.out.print("��"+i+"��������"); //���ÿһ������Ľ��
44+
for(int h=0;h<arr.length;h++){
45+
System.out.print(" "+arr[h]);
46+
}
47+
System.out.print("\n");
48+
}
49+
}
50+
51+
public static void main(String[]args){
52+
int[]shuzu=new int[SIZE];
53+
int i;
54+
for(i=0;i<SIZE;i++){
55+
shuzu[i]=(int)(100+Math.random()*(100+1)); //��ʼ������
56+
}
57+
System.out.print("����ǰ������Ϊ��\n"); //�������ǰ������
58+
for(i=0;i<SIZE;i++){
59+
System.out.print(" "+shuzu[i]);
60+
}
61+
System.out.print("\n");
62+
63+
select(shuzu); //ִ�������㷨
64+
System.out.print("���������������\n");
65+
for(i=0;i<SIZE;i++){
66+
System.out.print(" "+shuzu[i]);
67+
}
68+
System.out.print("\n");
69+
}
70+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package insertSort;
2+
3+
//ϣ���������ֱ�Ӳ��������нΘ�ĸĽ���ϣ�������ֽ���С��������
4+
//����˼�룺�Ƚ�����������ļ�1⁄4���зָ��Ϊ���������зֱ����ֱ�Ӳ������򣬴����������еļ�1⁄4����������ɦ���وٴ�ȫ���1⁄4��������ֱ�Ӳ�������
5+
/* �㷨����.
6+
* �Ƚ���������Ԫ�����зָ�����ɸ������У������ij������������Ԫ����ɵģ��ֱ����ֱ�Ӳ�������Ȼ���������������ٽ�������
7+
* �����������е�Ԫ�ػ������������㹻С��ɦ���وٴ�ȫ��Ԫ�ؽ���һ��ֱ�Ӳ���������Ϊֱ�Ӳ���������Ԫ�ػ������������£��ӽ�����������
8+
* Ч���Ǻܸߵģ�
9+
*/
10+
//��һ�ֺ͵ڶ���ֻ�Dz�ͬ��ֱ�Ӳ���д��������һ��
11+
public class ShellSort {
12+
13+
static final int SIZE = 10;
14+
15+
static void shellSort(int[] a) {
16+
// �Ϸ��Լ��
17+
if (a == null || a.length < 2) {
18+
return;
19+
}
20+
int i, j; // ����ѭ������
21+
int d, temp; // tempΪ�ݴ������dΪ���Ƿָ����������
22+
int x = 0; // ��1⁄4�����������������Ƿֳ���x��
23+
for (d = (a.length) / 2; d >= 1; d /= 2) {//������ֵIJ��������Ǽ���ÿ�����يٴ���һ���顣����һ��ʼ��ÿ����Ϊһ�顣
24+
25+
for (i = d; i < a.length; i++) { //�Լ�������IJ����������������ѭ���������ѭ���������Dz��ε��÷����������ƶ�������1��4Ϊһ��ģ�2��5Ϊһ��ġ���ôi++L·�ľ�������ȥ����ÿһ��
26+
temp = a[i];
27+
j = i - 1;
28+
// �ڷֺõ����ڴӴ���С���ҵ�һ����tmpС��Ԫ�أ�����tmp������󡣻���ֱ�Ӳ������
29+
for (; j >= 0; j--) {
30+
if (a[j] > temp) {
31+
a[j + 1] = a[j];
32+
} else {
33+
break;
34+
}
35+
}
36+
a[j + 1] = temp;
37+
}
38+
x++;
39+
System.out.print("��" + (x) + "��������"); // ���ÿһ������Ľ��
40+
for (int z = 0; z < a.length; z++) {
41+
System.out.print(" " + a[z]);
42+
}
43+
System.out.print("\n");
44+
}
45+
}
46+
47+
static void shellsort2(int a[]) {
48+
int j, gap;
49+
int x = 0; // ��1⁄4���������������Ƿ������
50+
51+
for (gap = a.length / 2; gap > 0; gap /= 2) {//������ֵIJ��������Ǽ���ÿ�����يٴ���һ���顣����һ��ʼ��ÿ����Ϊһ�顣
52+
for (j = gap; j < a.length; j++) {//�Լ�������IJ����������������ѭ���������ѭ���������Dz��ε��÷����������ƶ�������1��4Ϊһ��ģ�2��5Ϊһ��ġ���ôi++L·�ľ�������ȥ����ÿһ��
53+
//�����㣬�����������jô��Ҫ�أ��Ҿ��þ�Ҫ����������x���Ϊ������Ҳ�п�����ɺ�ɦͦ���ģ���Ϊ�����һ�飨��{1,9,2}������ĺ����и�һ����С�����֣�������ֱ���������������һ��ı���������Ҫgap=1��һ������ȫ�����l�
54+
if (a[j] < a[j - gap])
55+
{
56+
int temp = a[j];//�ݴ��������Ϊ����
57+
int k = j - gap;//��� a[j - gap]
58+
//ѭ����һ�飬���������ƶ�������a[k+gap]�ҵ�����λ�ã��Ӷ����в��롣ÿ��Ԫ�����Լ����ڵ����ݶԱȽ���ֱ�Ӳ�������
59+
while (k >= 0 && a[k] > temp) {
60+
a[k + gap] = a[k];
61+
k -= gap;
62+
}
63+
a[k + gap] = temp;//����
64+
}
65+
}
66+
x++;
67+
System.out.print("��" + (x) + "�����������������"); // ���ÿһ������Ľ��
68+
for (int z = 0; z < a.length; z++) {
69+
System.out.print(" " + a[z]);
70+
}
71+
System.out.print("\n");
72+
}
73+
}
74+
//ð��ϣ��
75+
static void bubbleShellSort(int a[]) {
76+
int i, j, gap;
77+
int x = 0; // ��1⁄4���������������Ƿ������������һ��ʼ��ÿ����Ϊһ�顣
78+
for (gap = a.length / 2; gap > 0; gap /= 2) {//������ֵIJ��������Ǽ���ÿ�����يٴ���һ���顣
79+
for (i = gap; i < a.length; i++) {//�Լ�������IJ����������������ѭ���������ѭ���������Dz��ε��÷����������ƶ�������1��4Ϊһ��ģ�2��5Ϊһ��ġ���ôi++L·�ľ�������ȥ����ÿһ��
80+
for (j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap) {//�ڷֺõ����н���ð�����򡣾����ж������������λ�ģ���ð�ݶ��顣
81+
int temp = a[j];
82+
a[j] = a[j + gap];
83+
a[j + gap] = temp;
84+
}
85+
}
86+
x++;
87+
System.out.print("��" + (x) + "�����������������"); // ���ÿһ������Ľ��
88+
for (int z = 0; z < a.length; z++) {
89+
System.out.print(" " + a[z]);
90+
}
91+
System.out.print("\n");
92+
}
93+
94+
}
95+
96+
public static void main(String[] args) {
97+
int a = (int) (10.5 / 5);
98+
// TODO Auto-generated method stub
99+
int[] shuzu = new int[SIZE];
100+
int i;
101+
for (i = 0; i < SIZE; i++) {
102+
shuzu[i] = (int) (100 + Math.random() * (100 + 1)); // ��ʼ������
103+
}
104+
System.out.print("����ǰ�����飺\n"); // �������ǰ������
105+
for (i = 0; i < SIZE; i++) {
106+
System.out.print(shuzu[i] + " ");
107+
}
108+
System.out.print("\n");
109+
110+
shellsort2(shuzu); // �����������
111+
112+
System.out.print("���������飺\n"); // �������������
113+
for (i = 0; i < SIZE; i++) {
114+
System.out.print(shuzu[i] + " ");
115+
}
116+
System.out.print("\n");
117+
}
118+
119+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
3+
//����˼·����1⁄4�ıȽϺ��ƶ��Ǵ��������м���еģ��ؼ���Θ�ļ�1⁄4һ�ξ��ܴ�ǰ���ƶ������棬
4+
// �ؼ����С�ļ�1⁄4һ�ξ��ܴӺ����ƶ���ǰ�棬��1⁄4�ƶ��ľ����Զ���Ӷ������ܵıȽΘ������ƶ�������
5+
// ������һ���������ӣ� ���������ݽṹ���㷨�ļ�����
6+
7+
8+
//���������ǣ�ѡ���һ����1⁄4�Ĺؼ�����Ϊ��ֵ
9+
public class QuickSort {
10+
11+
static final int SIZE=18;
12+
/** �õݹ��������
13+
*
14+
* @param arr ���������Ǵ���������
15+
* @param first ���������Ǵ������������ʼ�±�
16+
* @param end ���������Ǵ���������Ľ����±�
17+
*/
18+
19+
public static void quickSort(int[]arr,int first,int end){
20+
// �����Ϸ��Լ��
21+
if(arr==null||arr.length<2){
22+
return;
23+
}
24+
if(first<0||end<0||first>arr.length||end>arr.length|| first>=end){
25+
return;
26+
}
27+
if(first<end){
28+
int pivot=partition(arr,first,end); //����һ�λ���
29+
quickSort(arr,first,pivot-1); //����������н��еݹ�ؿ�������
30+
quickSort(arr,pivot+1,end); //���Ҳ������н��еݹ�ؿ�������
31+
}
32+
}
33+
34+
/** �����һ����׼��
35+
*
36+
* @param arr ���������Ǵ�����������߻��ֻ������������
37+
* @param first ���������Ǵ�����������߻��ֻ���������������ʼ�±�
38+
* @param end ���������Ǵ�����������߻��ֻ������������Ľ����±�
39+
* @return
40+
*/
41+
private static int partition(int[]arr,int first,int end){
42+
// �����Ϸ��Լ��
43+
if(arr==null||arr.length<2){
44+
return -1;
45+
}
46+
if(first<0||end<0||first>arr.length||end>arr.length|| first>end){
47+
return -1;
48+
}
49+
if (first == end) {
50+
return first;
51+
}
52+
int pivot = arr[first]; // ���������еĵ�һ��Ԫ��ѡ������
53+
while(first<end){
54+
while(first<end&&arr[end]>=pivot){ //�Ӻ��濪ʼѭ�������ұ�����ҪС��ֵ�������ŵ�ǰ��.ͬɦָ��ĩβ��ָ��Ҳ������ǰ�ƶ����ж����while��������ѭ����ֱ������ʼָ���غϣ��������������whileѭ��������
55+
end--;
56+
}
57+
arr[first]=arr[end];
58+
59+
while(first<end&&arr[first]<=pivot){ //�ӿ�ͷ��ʼѭ�������ұ�����Ҫ���ֵ�������ŵ����档ͬɦָ����ʼ��ָ��Ҳ��������ƶ����ж����while��������ѭ����ֱ����ĩβָ���غϣ��������������whileѭ��������
60+
first++;
61+
}
62+
arr[end]=arr[first];
63+
}
64+
arr[first]=pivot; //��first��end��ͣ����ҵ����εݹ������λ����
65+
return first;
66+
67+
}
68+
69+
70+
71+
public static void main(String[] args) {
72+
// TODO Auto-generated method stub
73+
int[] shuzu =new int[SIZE];
74+
int i;
75+
for(i=0;i<SIZE;i++){
76+
shuzu[i]=(int)(100+Math.random()*(100+1)); //��ʼ������
77+
}
78+
System.out.print("����ǰ�����飺\n"); //�������ǰ������
79+
for(i=0;i<SIZE;i++){
80+
System.out.print(shuzu[i]+" ");
81+
}
82+
System.out.print("\n");
83+
84+
quickSort(shuzu,0,SIZE-1); //�����������
85+
System.out.print("���������飺\n"); //�������������
86+
for(i=0;i<SIZE;i++){
87+
System.out.print(shuzu[i]+" ");
88+
}
89+
System.out.print("\n");
90+
}
91+
92+
}

‎基础知识/(6)排序/(6)堆排序.txt

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package heapSort;
2+
3+
//�ѵ����������������Ϊ�˵���������ѳ�������Ԫ��array[k]���������𲽵������νṹ
4+
/*
5+
* һ�㶼����������ʾ�ѣ�i���ĸ�����±��Ϊ(i �C 1) / 2��
6+
* ���������ӽ���±�ֱ�Ϊ2 * i + 1��2 * i + 2�����0����������ӽ���±�ֱ�Ϊ1��2��
7+
*/
8+
public class HeapSortTest {
9+
// �������Ľڵ�˳���ڽ��ѵ�ɦ������˹淶�Ե�����
10+
//��Ԫ��array[k]���������𲽵������νṹ
11+
/*
12+
* ������L·����ɶ���������L·�ľ��ǰ������Ǹ���Ū��������ȥ��С���Ѿͷ�������
13+
*/
14+
public void HeapAdjust(int[] array, int parent, int length) {
15+
int temp = array[parent];// temp���浱ǰ�����
16+
int child = 2 * parent + 1; // �Ȼ�ȡ����
17+
18+
while (child < length) {//childΪ��ʼ��Ϊ�ڵ�parent�����ӣ��ؽڵ�Θ���ӽڵ����μ���
19+
// ȡ�ڵ�Θ���ӽڵ���±꣬������Һ��ӽ�㣬�����Һ��ӽ���ֵ�������ӽ�㣬��ѡȡ�Һ��ӽ�㣬������һ�����Ľڵ����
20+
if (child + 1 < length && array[child] < array[child + 1]) {
21+
child++;//����ڵ���Һ���>���ӣ���ȡ�Һ��ӽڵ���±�
22+
}
23+
24+
// ��������parent��ֵ�Ѿ��������Լ��ĺ��ӽ���ֵ����ֱ�ӽ�������������
25+
if (temp >= array[child]){
26+
break;
27+
}else{
28+
29+
// �ҵ����ĺ��ӽڵ㣨�ȸ���㻹��ģ�,�Ѻ��ӽ���ֵ���������
30+
array[parent] = array[child];
31+
32+
// ѡȡ���ӽ������ӽ��,��������ɸѡ
33+
parent = child;//�Ѹ�����ָ����� ́��ݸ����ĺ��ӣ��Ա�������μ��������д���ѵ������Ѵ���������ȥ��
34+
child = 2 * child + 1;
35+
}
36+
System.out.format("���ѵ������̵�ϸ��(1) ");
37+
printPart(array, 0, array.length - 1);
38+
39+
}
40+
System.out.println("array[parent] ��"+array[parent]);
41+
System.out.println("temp ��"+temp);
42+
43+
array[parent] = temp;//�������Ľ���ֵ��������λ��
44+
45+
System.out.println("array[parent] �� ��"+array[parent]);
46+
System.out.println("temp �� ��"+temp);
47+
System.out.format("���ѵ������̵�ϸ��(2) ");
48+
printPart(array, 0, array.length - 1);
49+
System.out.format("------------------------------- \n ");
50+
51+
52+
}
53+
54+
public void heapSort(int[] list) {
55+
// **����** ѭ��������ʼ�ѣ������ķ��ڶѶ�
56+
//�����һ���ڵ�array.length-1�ĸ��ڵ㣨array.length-1-1��/2��ʼ��ֱ�����ڵ�0������������
57+
//û�м�2�����
58+
59+
for (int i = (list.length-2) / 2; i >= 0; i--) {
60+
HeapAdjust(list, i, list.length - 1);
61+
}
62+
63+
System.out.format("���ѵ������˵� ");
64+
printPart(list, 0, list.length - 1);
65+
// **������**������n-1��ѭ�����������
66+
for (int i = list.length - 1; i > 0; i--) {
67+
// ���һ��Ԫ�غ͵�һ��Ԫ�ؽ��н���
68+
int temp = list[i];
69+
list[i] = list[0];
70+
list[0] = temp;
71+
// ɸѡ R[0] ��㣬�õ�i-1�����Ķ�
72+
System.out.format("�� %d ��ǰ�Ľ���: \t", list.length - i);
73+
printPart(list, 0, list.length - 1);
74+
HeapAdjust(list, 0, i);
75+
System.out.format("�� %d ��: \t", list.length - i);
76+
printPart(list, 0, list.length - 1);
77+
}
78+
}
79+
80+
// ��ӡ����
81+
public void printPart(int[] list, int begin, int end) {
82+
for (int i = 0; i < begin; i++) {
83+
System.out.print("\t");
84+
}
85+
for (int i = begin; i <= end; i++) {
86+
System.out.print(list[i] + "\t");
87+
}
88+
System.out.println();
89+
}
90+
91+
public static void main(String[] args) {
92+
// TODO Auto-generated method stub
93+
// ��ʼ��һ������
94+
int[] array = { 1, 3, 4, 5, 2, 6, 9, 7, 8, 0 };
95+
// ���ÿ������򷽷�
96+
HeapSortTest heap = new HeapSortTest();
97+
System.out.print("����ǰ:\t");
98+
heap.printPart(array, 0, array.length - 1);
99+
heap.heapSort(array);
100+
System.out.print("�����:\t");
101+
heap.printPart(array, 0, array.length - 1);
102+
}
103+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /