在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
直接插入排序过程: 例: def insert_sort(lists): count = len(lists) for i in range(1, count): tmp = lists[i] j = i - 1 while j >= 0 and lists[j]>tmp: lists[j+1] = lists[j] j -= 1 lists[j+1] = tmp return lists
void direct_insert_sort(int *ar, int count); void direct_insert_sort(int *ar, int count){ int tmp; int i; int j; for (i=1; i < count; i++){ tmp = ar[i]; j = i-1; while(j>=0 && (ar[j] > tmp) ){ ar[j+1] = ar[j]; j--; } ar[j+1] = tmp; } }
希尔排序过程: 评价: def shell_sort(lists): count = len(lists) step = count/2 while step>0: for i in range(step, count, step): tmp = lists[i] j = i - step while j >= 0 and lists[j] > tmp: lists[j+step] = lists[j] j -= step lists[j+step] = tmp step/=2 return lists
void inner_direct_insert_sort(int *ar, int count, int step); void shell_sort(int *ar, int count); void shell_sort(int *ar, int count){ int step; for (step=count/2; step > 0; step/=2) inner_direct_insert_sort(ar, count, step); } // 调用插入排序,但是这里需要改变步长。 void inner_direct_insert_sort(int *ar, int count, int step){ int tmp; int i; int j; for (i=step; i < count; i+=step){ tmp = ar[i]; j = i-step; while(j>=0 && (ar[j] > tmp) ){ ar[j+step] = ar[j]; j-=step; } ar[j+step] = tmp; } } 冒泡排序:哈哈最简单了 1. 从头开始,依次和自己后面的元素进行比较,交换 时间复杂度也很高O(N^2) def bubble_sort(lists): count = len(lists) for i in range(0, count): for j in range(i+1, count) if lists[i] > lists[j]: lists[i], lists[j] = lists[j], lists[i] return lists
void bubble_sort(int *ar, int count); void bubble_sort(int *ar, int count){ int i; int j; int tmp; for(i=0; i<count; i++){ for (j=i+1; j<count; j++){ if(ar[i] > ar[j]){ tmp = ar[i]; ar[i] = ar[j]; ar[j] = tmp; } } } }
快速排序过程: 2、递归调用基本的步骤
最差情况:完全逆序、完全顺序 def quick_sort(lists, left, right): if left >= right: return lists tmp = lists[left] start = left end = right while left < right: while left < right and lists[right] > tmp: right -= 1 if left < right: lists[left] = lists[right] left += 1 while left < right and lists[left] < tmp: left += 1 if left < right: lists[right] = lists[left] right -= 1 lists[left] = tmp quick_sort(lists, start, left-1) quick_sort(lists, left+1, end) return lists
int base_action(int *ar, int start_index, int end_index); void inner_quick_sort(int *ar, int start_index, int end_index); void quick_sort(int *ar, int count); void quick_sort(int *ar, int count){ inner_quick_sort(ar, 0, count-1); } void inner_quick_sort(int *ar, int start_index, int end_index){ int mid_index; if(start_index < end_index){ mid_index = base_action(ar, start_index, end_index); inner_quick_sort(ar, start_index, mid_index-1); inner_quick_sort(ar, mid_index+1, end_index); } } int base_action(int *ar, int start_index, int end_index){ int tmp; tmp = ar[start_index]; while(start_index < end_index){ while(start_index < end_index && ar[end_index] > tmp){ end_index--; } if(start_index < end_index){ ar[start_index] = ar[end_index]; start_index++; } while(start_index < end_index && ar[start_index] < tmp){ start_index++; } if(start_index < end_index){ ar[end_index] = ar[start_index]; end_index--; } } ar[start_index] = tmp; return start_index; }
直接选择排序过程: 评价: def select_sort(lists): count = len(lists) for i in range(count): min_index = i for j in range(i+1,count): if lists[j] < lists[min_index]: min_index = j if min_index != i: lists[i], lists[min_index] = lists[min_index], lists[i] return lists
void direct_select_sort(int *ar, int count); void direct_select_sort(int *ar, int count){ int tmp; //用于交换的中间值 int i; //下一个要比较的元素,即参考元素 int j; //除了已排好序的集合和下一个元素,剩下的所有元素的都和下一个元素比较 int minIndex; //最小的值的下标,将来放到已排好的元素中去 for (i=0; i < count-1; i++){ minIndex = i; for (j = i+1; j<count; j++){ if(ar[j] < ar[minIndex] ){ minIndex = j; } } if (minIndex != i){ tmp = ar[minIndex]; ar[minIndex] = ar[i]; ar[i] = tmp; } } }
堆排序过程: 评价: def adjust_head(lists, root, count): not_finished = True while not_finished and root <= (count-1)/2: max_index = root left_child = 2*root + 1 right_child = 2*root + 2 if left_child < count and lists[left_child] > lists[max_index]: max_index = left_child if right_child < count and lists[right_child] > lists[max_index]: max_index = right_child if root != max_index: lists[root], lists[max_index] = lists[max_index], lists[root] else: not_finished = False root = max_index def heap_sort(lists): count = len(lists) last_not_leaf_node = (count-1)/2 for root in range(last_not_leaf_node, -1, -1): adjust_head(lists, root,count) #调整为大跟堆 while count > 1: lists[count-1], lists[0] = lists[0], lists[count-1] count -= 1 adjust_head(lists,root, count) return lists
void adjustBigHeap(int *ar, int count, int root); void heapSort(int *ar, int count); void heapSort(int *ar, int count){ int root; int tmp; for(root = (count-1)/2-1; root > 0; root--){ adjustBigHeap(ar, count, root); } while(count > 1){ adjustBigHeap(ar, count, root); tmp = ar[0]; ar[0] = ar[count-1]; ar[count-1] = tmp; count--; } } void adjustBigHeap(int *ar, int count, int root){ int maxIndex; int tmp; boolean finished = FALSE; while(!finished && maxIndex < (count-1)/2){ maxIndex = 2*root+1 < count && ar[2*root+1] > ar[root] ? 2*root+1 : root; maxIndex = 2*root+2 < count && ar[2*root+2] > ar[maxIndex] ? 2*root+2 : maxIndex; if(maxIndex != root){ tmp = ar[root]; ar[root] = ar[maxIndex]; ar[maxIndex] = tmp; }else{ finished = TRUE; } root = maxIndex; } }
归并算法:分而治之1. 将所有的数一分为二,在把其中的一组再一分为二,如此反复,最后在将有序的数组合并 2. 最关键的就是,递归终止的条件,即当每个组中只有一个元素。 3. 最重要的就是处理怎么合并。假设左边有序的起点为i, 右边有序的起点为j。将左右两边的数组相互比较,如果左边小的话,将比较的元素放入到结果集中,同时i应该加1。 如果右边元素大,则它放到结果集中,同时j要加1。如果左右两边其中的一边达到顶端(mid/end),则把另一组元素全部放到结果集中 评价: 平均情况 最坏情况 最好情况 空间复杂度 def merge(left, right): i, j = 0, 0 rst = [] while i < len(left) and j < len(right): if left[i] <= right[j]: rst.append(left[i]) i += 1 else: rst.append(right[j]) j += 1 rst += left[i:] rst += right[j:] return rst def merge_sort(lists): if len(lists) <= 1: return lists middle = len(lists)/2 left = merge_sort(lists[middle:]) right = merge_sort(lists[:middle]) return merge(left, right)
void merge_sort(int *ar, int left, int right); void merge(int *ar, int left, int right); void merge_sort(int *ar, int left, int right){ int i; if(left < right){ i = (left + right)/2; merge_sort(a, left, i); merge_sort(a, i+1, right); merge(a, left, right); } } void merge(int *ar, int left, int right){ int begin1 = left; int mid = (left + right)/2; int begin2 = mid+1; int k = 0; int new_ar_len = right - left + 1; int *b = (int *)malloc(new_ar_len*sizeof(int)); while(begin1 <= mid && begin2 <= right){ if(ar[begin1] <= ar[begin2]){ b[k++] = ar[begin1++]; }else { b[k++] = ar[begin2++]; } while(begin1 <= mid) b[k++] = ar[begin1++]; while(begin2 <= right) b[k++] = ar[begin2++]; copy_array(b, a, new_ar_len, left); free(b); } } void copy_array(int *src, int *dst, int new_ar_len, int first){ int i; int j=first; for(i=0; i < len; i++){ dst[j] = src[i]; j++; } }
基数排序:将所有的数,按照各位进行排序并保持数组,接着按照十位排序,进行百位... 桶的个数,应该是基数的大小 进行比较的次数=所有元素最大数所占的位数 def radix_sort(lists): for k in xrange(len(str(max(lists)))): bucket = [ [] for _ in xrange(10)] for i in lists: bucket[i / (10 ** k) % 10].append(i) lists = [element for item in bucket for element in item] return lists c语言我看着晕~
直接交换排序过程:
def exchange_sort(lists): count = len(lists) has_exchanged = True #在比较的过程中,如果没有数字发生交换,那么说明数据已经有序的了。退出即可 for i in range(0, count): for j in range(0, count-i-1): has_exchanged = False if lists[j] > lists[j+1]: lists[j], lists[j+1] = lists[j+1], lists[j] has_exchanged = True if not has_exchanged: break return lists
void direct_change_sort(int *ar, int count){ int i; int j; int tmp; unsigned char has_exchanged; for(i=0; i<count && has_exchanged ; i++){ for(j=0,has_exchanged = 0; j<count-i; j++){ if(ar[j] > ar[j+1]){ tmp = ar[j+1]; ar[j+1] = ar[j]; ar[j] = tmp; has_exchanged = 1; } } } }
偷张图:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论