基础排序算法——汇总

      本文集中汇总冒泡排序插入排序对半插入排序三种算法,并分别用C++C#Java三种语言实现。

冒泡排序:

C++ Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void BubbleSort(int* a,int n)  
{
int tmp;
for(int i=n;i>=1 ;i--)
{
for (int j = 0; j < i - 1; j++)
{
if (a[j] > a[j + 1]) //降序改为""
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}

C# Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void BubbleSort(int []array)  
{
int temp;
int count = array.Length;
for(int i=count;i>=1 ;i--)
{
for (int j = 0; j < i - 1; j++)
{
if (array[j] > array[j + 1]) //降序改为"<"
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void BubbleSort(List<Int32> array)
{
int temp;
int count = array.Count;
for (int i = count; i >= 1; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (array[j] > array[j + 1])//降序改为"<" 
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

Java Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void bubbleSort(int []array)  
{
int temp;
int count = array.length;
for(int i=count;i>=1 ;i--)
{
for (int j = 0; j < i - 1; j++)
{
if (array[j] > array[j + 1])//降序改为"<" 
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void bubbleSort(List<Integer>array)  
{
int temp;
int count = array.size();
for(int i=count;i>=1 ;i--)
{
for (int j = 0; j < i - 1; j++)
{
if (array.get(j) > array.get(j + 1)) //降序改为"<" 
{
temp = array.get(j);
array.set(j, array.get(j + 1));
array.set(j + 1,temp);
}
}
}
}

插入排序

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void InsertSort(int* a,int n)
{
int j = 0,temp;
for (int i = 1; i < n; i++)
{
temp = a[i];
j = i;
while (j > 0 && a[j - 1] >= temp)
{
a[j] = a[j - 1];
j -= 1;
}
a[j] = temp;
}
}

C# Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void InsertSort(int[] array)  
{
int j = 0,temp;
int count = array.Length;
for (int i = 1; i < count; i++)
{
temp = array[i];
j = i;
while (j > 0 && array[j - 1] >= temp)
{
array[j] = array[j - 1];
j -= 1;
}
array[j] = temp;
PrintArray(array);
}
}

Java Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void InsertSort(int[] array)  
{
int j = 0,temp;
int count = array.length;
for (int i = 1; i < count; i++)
{
temp = array[i];
j = i;
while (j > 0 && array[j - 1] >= temp)
{
array[j] = array[j - 1];
j -= 1;
}
array[j] = temp;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void InsertSort(List<Integer> array)  
{
int j = 0,temp;
int count = array.size();
for (int i = 1; i < count; i++)
{
temp = array.get(i);
j = i;
while (j > 0 && array.get(j - 1) >= temp)
{
array.set(j, array.get(j-1));
j -= 1;
}
array.set(j, temp);
}
}

对半插入排序算法

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void HalfInsertSort(int* a,int n)
{
int j = 0, temp, low, high, mid;
for (int i = 1; i < n; i++)
{
temp = a[i];
low = 0;
high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (temp < a[mid]) high = mid - 1;
else low = mid + 1;
}
for (j = i - 1; j >= low; j--) a[j + 1] = a[j];
a[low] = temp;
}
}

C# Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void HalfInsertSort(int[] array)
{
int j = 0, temp, low, high, mid;
int count = array.Length;
for (int i = 1; i < count; i++)
{
temp = array[i];
low = 0;
high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (temp < array[mid]) high = mid - 1;
else low = mid + 1;
}
for (j = i - 1; j >= low; j--) array[j + 1] = array[j];
array[low] = temp;
}
}

Java Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void HalfInsertSort(int[] array)
{
int j = 0, temp, low, high, mid;
int count = array.length;
for (int i = 1; i < count; i++)
{
temp = array[i];
low = 0;
high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (temp < array[mid]) high = mid - 1;
else low = mid + 1;
}
for (j = i - 1; j >= low; j--) array[j + 1] = array[j];
array[low] = temp;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void HalfInsertSort(List<Integer> array)
{
int j = 0, temp, low, high, mid;
int count = array.size();
for (int i = 1; i < count; i++)
{
temp = array.get(i);
low = 0;
high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (temp < array.get(mid)) high = mid - 1;
else low = mid + 1;
}
for (j = i - 1; j >= low; j--) array.set(j+1,array.get(j));
array.set(low, temp);
}
}

反馈请点击: