Algorithm--Insertion Sort

本文深入探讨了插入排序算法的原理、复杂度分析及实际应用。通过代码示例展示了如何实现插入排序,并通过性能测试验证了其在特定场景下的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插入排序
package practice1;
public class InserSort {
	/**
	 * Insertion Sort: 每次把一个项按照关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部插入完成
	 * 时间复杂度:T(n) = O(n^2)
	 * 在输入规模较小的情况下,使用插入排序
	 * @param array
	 */
	public static int[] insertSort(int[] array){
		int j, current;
		for(int i=1; i<array.length; i++){
			current = array[i];
			j = i-1;
			while (j >= 0 && current < array[j]) {
				array[j+1] = array[j];
				j--;
			}
			array[j+1] = current;
		}
		return array;
	}
	/**
	 * print the array
	 * @param arr
	 */
	public static void printArray(int[] arr){
		for(int i = 0;i<arr.length;i++){
			System.out.println(i+"---"+arr[i]);
		}
	}
	/**
	 * produce random array
	 * @param num
	 * @return
	 */
	private static int[] produceIntList(int num) {
		int list[] = new int[num];
		for(int i=0; i<num; i++){
			int temp = (int) (Math.random() * 10000);
			list[i] = temp;
		}
		return list;
	}
	public static void main(String[] args) {
		int[] list = produceIntList(100000);
		long time1 = System.currentTimeMillis();
		list = insertSort(list);
		long time2 = System.currentTimeMillis();
		System.out.println("the time used is : "+(time2 - time1));
	}
	
}

### Hybrid Sort Algorithm and Total Cache Miss Hybrid sorting algorithms combine two or more different sorting methods to achieve better performance characteristics than using a single approach alone. One common hybrid sort is TimSort, which combines merge sort and insertion sort techniques[^1]. The choice of combining specific sorts depends on the data distribution and size. In computer science, cache misses occur when the processor cannot find required data in the cache memory, leading to fetching from slower main memory. This phenomenon significantly impacts program execution speed. Total Cache Miss (TCM), therefore, refers to all instances where requested data must be retrieved directly from RAM instead of faster caches. The relationship between hybrid sort algorithms and total cache miss involves optimizing how elements are accessed during sorting operations: - **Data Access Patterns**: Efficient hybrids like TimSort aim at minimizing random accesses by leveraging natural runs within datasets, thereby reducing TCMs. - **Memory Hierarchy Utilization**: By adapting based on input sizes and patterns, these algorithms can better utilize multi-level caching architectures present in modern CPUs, thus lowering overall TCM rates. For implementing such an optimization-focused version of a hybrid sort considering TCM reduction: ```python def optimized_hybrid_sort(arr): min_run = calculate_minrun(len(arr)) # Calculate optimal run length for start in range(0, len(arr), min_run): end = min(start + min_run - 1, len(arr) - 1) # Insertion sort each small segment individually insertion_sort(arr, start, end) size = min_run while size < len(arr): for left in range(0, len(arr), 2 * size): mid = min((left + size - 1), (len(arr)-1)) right = min((left + 2*size - 1), (len(arr)-1)) if mid < right: merge(arr, left, mid, right) size *= 2 def calculate_minrun(n): r = 0 while n >= MIN_MERGE: r |= n & 1 n >>= 1 return n + r ``` This code snippet demonstrates part of what might go into creating a hybrid sorter that considers both efficiency gains through adaptive behavior as well as potential reductions in total cache misses due to improved locality properties.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值