Interview - mergesort
来源:互联网 发布:石家庄那里淘宝拍图好 编辑:程序博客网 时间:2024/06/16 11:38
Merging with smaller auxiliary array. Suppose that the subarray a[0] to a[n−1] is sorted and the subarray a[n] to a[2∗n−1] is sorted. How can you merge the two subarrays so that a[0] to a[2∗n−1] is sorted using an auxiliary array of length n (instead of 2n)?
描述: 使用n而不是2n的额外存储空间来对数列的两个子数列进行归并。
解答: 设辅助数组为aux[], 将a[]的前半个子数列拷贝到aux[],然后根据aux[i](i从0开始)和a[j](j从n开始)的大小关系往a[k](k从0开始)里面放置相应元素。Counting inversions. An inversion in an array a[] is a pair of entries a[i] and a[j] such that i < j but a[i]>a[j]. Given an array, design a linearithmic algorithm to count the number of inversions.
描述: 计算数列中的逆序对的个数,要求算法的复杂度是线性对数的。
解答: 事实上数列里面逆序对的个数就等于排序里面需要执行交换操作的次数。使用递归的思想,先求出左边子数列的逆序数,再求出右边子数列的逆序数,加上左右两边因为分开元素产生的逆序数,就得到了总的逆序数。主要是针对归并排序作修改,在merge方法里面加入逆序数count的操作,count += 此轮判定的逆序数。Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items uniformly at random. Your algorithm should consume a logarithmic (or constant) amount of extra memory and run in time proportional to nlogn in the worst case.
描述: 对一个链表进行随机化操作。
解答: 看stackoverflow吧,这个我也比较迷糊。stackoverflow
- Interview - mergesort
- MergeSort
- mergesort
- MergeSort
- mergesort
- mergesort
- Mergesort
- mergesort
- mergeSort
- mergeSort
- mergesort
- MergeSort
- MergeSort
- MergeSort
- MergeSort
- Mergesort
- Mergesort
- mergeSort
- android写一个图片选择
- 数据重定向、管道及后台运行程序
- eclipse 启动过程(仅记录主要入口)
- 待解决的问题
- Hausdorff距离匹配算法及代码,特征点进行匹配得出目标区域,但是不具有区域自适应。
- Interview - mergesort
- 分解质因素的Java递归算法
- 论项目经理在项目中的重要性(实际项目问题总结)
- 消除xcode8中Empty paragraph passed to '***' command的警告
- Android OTG-U盘文件读写
- bootstrap-table 表头,和内容对齐问题
- java 网络编程。
- 【面试题】java类加载机制探索
- word 中的修订模式