Merge Sort and apply it to an interview question
来源:互联网 发布:回收站数据恢复破解版 编辑:程序博客网 时间:2024/06/05 05:55
Merge Sort is very efficient in sorting a list of elements with O(nlgn) complexity. It is used with divide and conquer strategy.
A few days ago, I came to an interview question, the description of the question is below:
There are two collections, list1 and list2, each collection has more than 10000 elements.
Please implement a method in Java without calling the system API; this method should return
a collection which contains all the elements of list1 and list2 and has no duplicate elements.
The meaning of this question is to get the union of two sets.There are two points should be paid attention, first the duplicated elements in list1 and list2.The second is the duplicated elements in list1 or list2. We can implement a hash table to solve this issue. I try to solve this problem with merge sort idea. First I just want to merge the two lists but if you don't consider the order of the two lists, this method can't eliminate all the duplicate element.
Below is the first version code in Python:
def merge_set(left, right): result = [] i, j = 0, 0 while i < len(left) and j < len(right): if left[i] != right[j]: result.append(left[i]) result.append(right[j]) else: result.append(left[i]) i += 1 j += 1 while i < len(left): result.append(left[i]) i += 1 while j < len(right): result.append(right[j]) j += 1 return resultdef union_set(S): n = len(S) if n <= 1: return S # divide mid = int(len(S) / 2) #n // 2 L1 = S[0:mid] L2 = S[mid:n] # conquer with recursion left = union_set(L1) right = union_set(L2) # merge resultx return merge_set(left, right) if __name__ == '__main__': S = [10, 5, 5, 5, 7] #merge_sort(S) L = union_set(S) print L
It got problem when running the test, because the order of two lists.
Belowis the code that can work fine withtwo lists, duplicated or no duplicated.
def merge(left, right): result = [] i, j = 0, 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 elif left[i] == right[j]: # do some modification when they are equal result.append(left[i]) i += 1 j += 1 else: result.append(right[j]) j += 1 result += left[i:] result += right[j:] return resultdef mergeset(lst): if len(lst) <= 1: return lst mid = int(len(lst) / 2) left = mergeset(lst[:mid]) right = mergeset(lst[mid:]) return merge(left, right)if __name__ == "__main__": #L = mergeset([10, 5, 5, 5, 7]) lst1 = [10, 5, 5, 5, 7] lst2 = [10, 5, 8, 8, 9, 11, 11, 12, 12] lst = lst1 + lst2 L = mergeset(lst) print L# A = ['Hi', 'Ah', 'Wo', 'Hi', 'Hi']# L = mergeset(A)# print L
Below is the merge sort with java and python code,I paste here just for a record.
package hello.sorting;import java.util.ArrayList;import java.util.List;import java.util.Random;public class MergeSort<T> {// the list to be sortedprivate List<T> lst;public MergeSort() {//lst = new ArrayList<T>(n);//this.lst = lst;}public void init(List<T> lst) {this.lst = lst;}/** * merge two lists * @param left * @param right * @return */@SuppressWarnings({ "unchecked", "rawtypes" })public List<T> merge(List<T> left, List<T> right) {List<T> result = new ArrayList<T>();int i = 0, j = 0;while(i < left.size() && j < right.size()) {if(((Comparable)left.get(i)).compareTo(((Comparable)right.get(j))) <= 0) {result.add(left.get(i));i += 1;}else {result.add(right.get(j));j += 1;}}while(i < left.size()) {result.add(left.get(i));i++;} while(j < right.size()) {result.add(right.get(j));j++;}return result;}/** * merge sort with dividing and conquering * @param lst * @return */public List<T> mergeSort(List<T> lst) {if(lst.size() <= 1) {return lst;}// divideint mid = (int)(lst.size() / 2);List<T> L = lst.subList(0, mid);List<T> R = lst.subList(mid, lst.size());// conquer with recursionList<T> left = mergeSort(L);List<T> right = mergeSort(R);// merge the left and right listreturn merge(left, right);}/** * generate a random integer list less than 100 * @param n * @return */public List<Integer> generateRandom(int n) {List<Integer> list = new ArrayList<Integer>();Random r = new Random();for(int i = 0; i < n; i++) {list.add(r.nextInt() % 100);}return list;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMergeSort<Integer> ms = new MergeSort<Integer>();List<Integer> lst = ms.generateRandom(10);for(int i = 0; i < lst.size(); i++) {System.out.print(" " + lst.get(i));}System.out.println();ms.init(lst);List<Integer> result = ms.mergeSort(lst);for(int i = 0; i < result.size(); i++) {System.out.print(" " + result.get(i));}System.out.println();System.out.println("Ha..");}}
Python code:
def merge(S1, S2, S): ''' Merge two sorted python lists S1 and S2 into properly size list S. ''' i = j = 0 while i + j < len(S): if j == len(S2) or (i < len(S1) and S1[i] < S2[j]): S[i + j] = S1[i] i += 1 else: S[i + j] = S2[j] j += 1def merge_sort(S): ''' Sort the elements of python list S using merge-sort algorithm ''' n = len(S) if n < 2: return # divide mid = n // 2 S1 = S[0:mid] S2 = S[mid:n] # conquer with recursion merge_sort(S1) merge_sort(S2) # merge result merge(S1, S2, S)
- Merge Sort and apply it to an interview question
- Magento. How to create an attribute and apply it to products
- an interview question about fork
- Python Interview Question and Answers
- How to sort an NSMutableArray with custom objects in it?
- Interview question
- interview question
- interview question
- interview question
- interview-question
- Insertion Sort and Merge Sort
- Insertion sort and Merge sort
- Insertion Sort and Merge Sort
- Merge Sort and Inversion
- C# and ASP.NET Interview Question and Answers
- Launch an NSTask and bring it to front
- Top 10 Struts Interview Question And Answer - J2EE
- Difference between Set, List and Map in Java - Interview question
- 隐藏 UITUITababBar (解决底部空白问题)
- hdu1251 统计难题 (字典树)
- StringTokenizer
- Centos上安装MariaDB
- HDU 2255 奔小康赚大钱 求最大匹配值(入门)
- Merge Sort and apply it to an interview question
- (poj)3254 Corn Fields
- Hadoop namenode 没有运行 解决方案
- 父shell和子shell的区别,在shell脚本中要cd一个文件夹为什么要用父shell运行
- Codeforces Round #196 (Div. 2) C. Quiz
- 使用SecureCRT 连接VMWare8中的linux RedHat9.0/CentOS6.2虚拟机
- java下导入excel用到方法以及jar包
- uboot1.1.6移植SDHC驱动、Fat支持
- 【美化§俘获云朵xp电脑主题】