Merge remote-tracking branch 'upstream/master'

This commit is contained in:
nameczz 2018-10-22 17:02:17 +08:00
commit 2226604af7
4 changed files with 232 additions and 25 deletions

139
c-cpp/11_sorts/sorts.c Normal file
View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct array {
int size;
int used;
int *arr;
};
void dump(struct array *array)
{
int idx;
for (idx = 0; idx < array->used; idx++)
printf("[%02d]: %08d\n", idx, array->arr[idx]);
}
void alloc(struct array *array)
{
array->arr = (int *)malloc(array->size * sizeof(int));
}
void bubble_sort(struct array *array)
{
int i, j;
if (array->used <= 1)
return;
for (i = 0; i < array->used; i++) {
bool has_swap = false;
for (j = 0; j < array->used - i - 1; j++) {
if (array->arr[j] > array->arr[j+1]) {
int tmp;
tmp = array->arr[j];
array->arr[j] = array->arr[j+1];
array->arr[j+1] = tmp;
has_swap = true;
}
}
if (!has_swap)
break;
}
}
void bubble_sort_test()
{
int idx;
struct array ten_int = {10, 0, NULL};
alloc(&ten_int);
for (idx = 0; idx < 10; idx++)
ten_int.arr[idx] = 30 - idx;
ten_int.used = 10;
dump(&ten_int);
bubble_sort(&ten_int);
dump(&ten_int);
}
void insertion_sort(struct array *array)
{
int i, j;
if (array->used <= 1)
return;
for (i = 1; i < array->used; i++) {
int val = array->arr[i];
for (j = i - 1; j >= 0; j--) {
if (val < array->arr[j])
array->arr[j+1] = array->arr[j];
else
break;
}
array->arr[j+1] = val;
}
}
void insertion_sort_test()
{
int idx;
struct array ten_int = {10, 0, NULL};
alloc(&ten_int);
for (idx = 0; idx < 10; idx++)
ten_int.arr[idx] = 30 - idx;
ten_int.used = 10;
dump(&ten_int);
insertion_sort(&ten_int);
dump(&ten_int);
}
void selection_sort(struct array *array)
{
int i, j;
if (array->used <= 1)
return;
for (i = 0; i < array->used - 1; i++) {
int tmp, idx = i;
for (j = i + 1; j < array->used; j++)
if (array->arr[j] < array->arr[idx])
idx = j;
if (idx == i)
continue;
tmp = array->arr[i];
array->arr[i] = array->arr[idx];
array->arr[idx] = tmp;
}
}
void selection_sort_test()
{
int idx;
struct array ten_int = {10, 0, NULL};
alloc(&ten_int);
for (idx = 0; idx < 10; idx++)
ten_int.arr[idx] = 30 - idx;
ten_int.used = 10;
dump(&ten_int);
selection_sort(&ten_int);
dump(&ten_int);
}
int main()
{
//bubble_sort_test();
//selection_sort_test();
insertion_sort_test();
return 0;
}

View File

@ -1,54 +1,66 @@
package _1_sorts
func BubbleSort(a []int) {
arrLen := len(a)
if arrLen <= 1 {
/*
冒泡排序插入排序选择排序
*/
//冒泡排序a是数组n表示数组大小
func BubbleSort(a []int, n int) {
if n <= 1 {
return
}
for i := arrLen - 1; i > 0; i-- {
for j := 0; j < i; j++ {
for i := 0; i < n; i++ {
// 提前退出标志
flag := false
for j := 0; j < n-i-1; j++ {
if a[j] > a[j+1] {
tmp := a[j+1]
a[j+1] = a[j]
a[j] = tmp
a[j], a[j+1] = a[j+1], a[j]
//此次冒泡有数据交换
flag = true
}
}
// 如果没有交换数据,提前退出
if !flag {
break
}
}
}
func InsertSort(a []int) {
arrLen := len(a)
if arrLen <= 1 {
// 插入排序a表示数组n表示数组大小
func InsertionSort(a []int, n int) {
if n <= 1 {
return
}
for i := 1; i < arrLen; i++ {
v := a[i]
for i := 1; i < n; i++ {
value := a[i]
j := i - 1
//查找要插入的位置并移动数据
for ; j >= 0; j-- {
if a[j] > v {
if a[j] > value {
a[j+1] = a[j]
} else {
break
}
}
a[j+1] = v
a[j+1] = value
}
}
func SelectionSort(a []int) {
arrLen := len(a)
if arrLen <= 1 {
// 选择排序a表示数组n表示数组大小
func SelectionSort(a []int, n int) {
if n <= 1 {
return
}
for i := 0; i < arrLen; i++ {
for i := 0; i < n; i++ {
// 查找最小值
minIndex := i
for j := i + 1; j < arrLen; j++ {
for j := i + 1; j < n; j++ {
if a[j] < a[minIndex] {
minIndex = j
}
}
if minIndex != i {
tmp := a[minIndex]
a[minIndex] = a[i]
a[i] = tmp
}
// 交换
a[i], a[minIndex] = a[minIndex],a[i]
}
}

View File

@ -58,6 +58,9 @@ public class Sorts {
}
}
if (minIndex == i)
continue;
// 交换
int tmp = a[i];
a[i] = a[minIndex];

View File

@ -0,0 +1,53 @@
package sorts;
/**
* 计数排序
*
* Author: ZHENG
*/
public class CountingSort {
// 计数排序a是数组n是数组大小假设数组中存储的都是非负整数
public static void countingSort(int[] a, int n) {
if (n <= 1) return;
// 查找数组中数据的范围
int max = a[0];
for (int i = 1; i < n; ++i) {
if (max < a[i]) {
max = a[i];
}
}
// 申请一个计数数组c下标大小[0,max]
int[] c = new int[max + 1];
for (int i = 0; i < max + 1; ++i) {
c[i] = 0;
}
// 计算每个元素的个数放入c中
for (int i = 0; i < n; ++i) {
c[a[i]]++;
}
// 依次累加
for (int i = 1; i < max + 1; ++i) {
c[i] = c[i-1] + c[i];
}
// 临时数组r存储排序之后的结果
int[] r = new int[n];
// 计算排序的关键步骤了有点难理解
for (int i = n - 1; i >= 0; --i) {
int index = c[a[i]]-1;
r[index] = a[i];
c[a[i]]--;
}
// 将结果拷贝会a数组
for (int i = 0; i < n; ++i) {
a[i] = r[i];
}
}
}