1.冒泡算法
首先遍历整个数组,从前往后依次判断相邻两个元素数值大小,若后一个元素小于前一个元素,则将二者调换位置,那么遍历之后数组最后一个元素将是整个数组中的最大值。然后再遍历前n-1个元素找到最大值放在最右端,再进入下一轮如此往复直至剩下最后一个元素,结束,该过程总共要进行n -1轮遍历。
public void bubbleSort(int[] arr){
if (arr == null || arr.length < 2){
return;
}
else{
for(int i = 0; i < arr.length - 1; i++){
for(int j = 0; j < arr.length - 1 - i; j++)
if(arr[j] > arr[j + 1]){
swap(arr, j, j + 1);
}
}
}
}
}
public void swap(int[] arr, int i, int j){
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
2.选择算法
先遍历整个数组,找出数组中值最小的元素,放在最前面,此过程先假设最小索引为0,再和后面的元素依次判断,不停更新索引。然后依次找出后面元素的最小值放在索引为1,2,3....n-1的位置。
public void chooseSort(int[] arr){
if (arr == null || arr.length < 2){
return;
}
else {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
minIndex = arr[minIndex] < arr[j] ? minIndex : j;
}
swap(arr, i, minIndex);
}
}
}
public void swap(int[] arr, int i, int j){
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
3.插值算法
插值算法从数组的第二个元素开始,判断和前面元素关系,调整顺序,保证前两个元素有序,然后再从第三个元素往前看寻找合适的位置插入,保证前三个元素有序,如此往复,最后使得所有元素保证有序。
public void insertSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
} else {
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
swap(arr, j - 1, j);
}
}
}
}
public void swap(int[] arr, int i, int j) {
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
4.归并算法
主要思想是采用二分,递归将各自一半的元素排好顺序,再将两半的元素融合到一起,从而得到 最终的顺序数组。
#include<iostream>
using namespace std;
int process(int[], int, int);
int merge(int[], int, int);
int main() {
int arr[] = { 1, 9, 3, 4, 5, 6, 7 };
process(arr, 0, 6);
for (int i = 0; i < 7; i++) {
cout << arr[i] << "\t";
}
system("pause");
return 0;
}
int process(int arr[],int l, int r) {
if (l == r) {
return 0;
}
else {
int mid = l + ((r - l) >> 1);
process(arr, l, mid);
process(arr, mid + 1, r);
merge(arr, l, r);
return 0;
}
}
int merge(int arr[], int l, int r) {
int i = l;
int b = 0;//help数组起始
int mid = l + ((r - l) >> 1);
int j = mid + 1;
int*help = (int*)malloc((r - l + 1)*sizeof(int));
while (i <= mid && j <= r) {
help[b++] = arr[i] < arr[j] ? arr[i++] : arr[j++];
}
while (i <= mid) {
help[b++] = arr[i++];
}
while (j <= r) {
help[b++] = arr[j++];
}
for (int p = 0; p < r - l + 1; p++){
arr[l + p] = help[p];
}
return 0;
}
我们可以借助归并算法计算小和,主要思想是在每一次融合时判断右侧是否比左侧大,若大则在小和增加右侧数量的左侧数字大小。
#include<iostream>
using namespace std;
int process(int *arr, int l, int r, int &sum);
int merge(int *arr, int l, int r, int &sum);
int main() {
int arr[] = { 2, 1 , 3, 5, 4, 7};
int sum = 0;
int len = sizeof(arr) / sizeof(int);
process(arr, 0, len - 1, sum);
cout << "小和为" << sum << endl;
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
system("pause");
}
int process(int *arr, int l, int r, int &sum) {
if (l == r) {
return 0;
}
else {
int mid = l + ((r - l) >> 1);
process(arr, l, mid, sum);
process(arr, mid + 1, r, sum);
merge(arr, l, r, sum);
}
return 0;
}
int merge(int *arr, int l, int r, int &sum) {
int*help = (int*)malloc((r - l + 1)*sizeof(int));
int mid = l + ((r - l) >> 1);
int i = l;
int j = mid + 1;
int h = 0;
while (i <= mid && j <= r) {
sum += arr[i] < arr[j] ? arr[i] * (r - j + 1) : 0;
help[h++] = arr[i] < arr[j] ? arr[i++] : arr[j++];
}
while (i <= mid) {
help[h++] = arr[i++];
}
while (j <= r) {
help[h++] = arr[j++];
}
for (int t = 0; t < r - l + 1; t++) {
arr[l + t] = help[t];
}
return sum;
}
5.快速排序
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
#define PI 3.14
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int* partition(int* arr, int l, int r) {
int small = l - 1;
int big = r;
srand((unsigned int)time(NULL));
int midnum = l + rand() % (r - l + 1);
swap(arr[midnum], arr[r]);
int i = l;
while (i < big) {
if (arr[i] < arr[r]) {
swap(arr[i++], arr[++small]);
}
else if (arr[i] > arr[r]) {
swap(arr[i], arr[--big]);
}
else{
i++;
}
}
swap(arr[r], arr[big]);
return new int [2]{ small,big };
}
void quickSort(int* arr, int l, int r) {
if (l < r) {
int a0 = arr[0];
int a1 = arr[1];
int a2 = arr[2];
int a3 = arr[3];
int a4 = arr[4];
int *p1 = partition(arr, l, r);
quickSort(arr, l, p1[0]);
quickSort(arr, p1[1] + 1, r);
}
}
int main() {
int arr[] = { 2,9, 3,6,8,1,5,3,7,2,2,5};
int len = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, len - 1);
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
}
6.堆排序
主要思想是先排好一个大根堆,将大根堆第一个元素与最后元素交换,再将数组排成大根堆,再与倒数第二个元素交换,如此循环,直至与第二个元素交换,即可排好。
#include <iostream>
#include<string>
using namespace std;
void swap(int* arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void heapInsert(int* arr, int index){
while(arr[index] > arr[(index - 1)/2]){
swap(arr, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
void heapify(int* arr, int index, int heapsize){
int left = index * 2 + 1;
int largest;
while(left < heapsize){
largest = arr[left + 1] > arr[left] && left + 1 < heapsize ? left + 1 : left;
if (arr[largest] > arr[index]){
swap(arr, largest, index);
index = largest;
left = index * 2 + 1;
}
else {
break;
}
}
}
void heapSort(int* arr, int heapsize){
if (heapsize == 1){
return;
}
else{
for (int i = 0; i < heapsize; i++){
heapInsert(arr, i);
}
swap(arr, 0, --heapsize);
while (heapsize > 0){
heapify(arr, 0, heapsize);
swap(arr, 0, --heapsize);
}
}
}
int main() {
int arr[] = {7,6,5,4,3,2,1};
heapSort(arr,7);
for (int i = 0; i < 7; i++){
cout << arr[i] << " ";
}
}
7.桶排序
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>
int getdit(T& arr){
int dit = 0;
int except = 1;
int dit1 = 0;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){
dit1 = 0;
while (arr[i] / except != 0){
dit1++;
except *= 10;
}
if (dit1 > dit){
dit = dit1;
}
}
return dit;
}
int getdit(int num, int d){
int res = num;
for(int i = 0; i < d; i++){
res = res / 10;
}
return res == 0 ? res : res % 10;;
}
template<class T>
void bucketSort(T& arr){
int len = sizeof(arr) / sizeof(arr[0]);
int con_bit = getdit(arr);
int help[7];
// int count[10] = {0,0,0,0,0,0,0,0,0,0};
int dit;
for (int i = 0; i < con_bit; i++){
int count[10] = {0,0,0,0,0,0,0,0,0,0};
for (int j = 0; j < len; j++){
dit = getdit(arr[j],i);
count[dit]++;
}
for (int d = 1; d < 10; d++){
count[d] += count[d - 1];
}
for (int j = len - 1; j >= 0; j--){
dit = getdit(arr[j], i);
help[--count[dit]] = arr[j];
// count[dit]--;
}
for (int j = 0; j < len; j++){
arr[j] = help[j];
}
}
}
int main() {
int arr[] = {224,25,43,16,182,77,14};
bucketSort(arr);
for (int i = 0; i < 7; i++){
cout << arr[i] << " ";
}
}