题目链接:Running Time of Quicksort

Challenge 
In practice, how much faster is Quicksort (in-place) than Insertion Sort? Compare the running time of the two algorithms by counting how many swaps or shifts each one takes to sort an array, and output the difference. You can modify your previous sorting code to keep track of the swaps. The number of swaps required by Quicksort to sort any given input have to be calculated. Keep in mind that the last element of a block is chosen as the pivot, and that the array is sorted in-place as demonstrated in the explanation below.

Any time a number is smaller than the partition, it should be "swapped", even if it doesn't actually move to a different location. Also ensure that you count the swap when the pivot is moved into place. The count for Insertion Sort should be the same as the previous challenge, where you just count the number of "shifts".

Input Format 
There will be two lines of input:

  • n - the size of the array
  • ar - n numbers that makes up the array

Output Format 
Output one integer D, where D = (insertion sort shifts) - (quicksort swaps)

Constraints 
1<=s<=1000 
-1000<=x<= 1000 , x ∈ ar

Sample Input

7
1 3 9 8 2 7 5

Sample Output

1

Explanation 
Insertion Sort will take 9 "shifts" to sort the array. Quicksort will take 8 "swaps" to sort it, as shown in the diagram below. 9-8 = 1, the output.


题解:统计排序中快速排序和插入排序元素移动次数的差。对于插入排序,统计元素移动的次数;对于快速排序统计元素交换的次数(包括自己跟自己交换),然后输出二者之差。

对于插入排序元素的移动次数可以参见:Insertion Sort Advanced Analysis,不过这道题暴力可能也可以过,有现成的代码就拿来用了。

对于快速排序,直接在排序过程中统计交换次数就可以了,上述引自HackerRank的图很好的说明了快速排序中Partition的工作过程。

最终代码如下:

 import java.util.*;

 public class Solution {
private static long answer = 0;
private static long swaps = 0;
private static void swap(int[] ar,int i,int j){
swaps++;
int temp = ar[i];
ar[i] = ar[j];
ar[j]= temp;
return;
}
private static int Partition(int[] ar,int start,int end){
int pivot = ar[end];
int i = start;
int j = start;
while(i<end){
if(ar[i]< pivot ){
swap(ar,i,j);
i++;
j++;
}
else {
i++;
}
}
swap(ar,j, end);
return j;
} private static void quickSort(int[] ar,int start,int end){
if(start >= end)
return;
int pivot = Partition(ar,start,end);
quickSort(ar,start,pivot-1);
quickSort(ar, pivot+1, end);
}
private static int[] Merge(int[] ar1,int[] ar2){
int m = ar1.length;
int n = ar2.length; int point1 = 0;
int point2 = 0;
int index_result = 0;
int[] result = new int[m+n];
while(point1 < m && point2 < n){
if(ar1[point1] < ar2[point2]){
result[index_result] = ar1[point1];
point1++;
index_result++;
}
else if(ar1[point1] > ar2[point2]){
answer += m - point1;
result[index_result] = ar2[point2];
index_result++;
point2++;
}
else{
result[index_result] = ar1[point1];
index_result++;
point1++;
}
}
while(point1 < m){
result[index_result] = ar1[point1];
index_result++;
point1++;
}
while(point2 < n){
answer += m - point1;
result[index_result] = ar2[point2];
index_result++;
point2++;
}
return result;
}
private static int[] mergeSort(int[] ar){
int n = ar.length;
if(n <= 1)
return ar;
int mid = n/2;
int[] ar1 = new int[mid];
int[] ar2 = new int[n-mid];
System.arraycopy(ar, 0, ar1, 0, mid);
System.arraycopy(ar, mid, ar2, 0, n-mid);
int[] sorted_ar1 = mergeSort(ar1);
int[] sorted_ar2 = mergeSort(ar2);
int[] result = Merge(sorted_ar1, sorted_ar2);
return result;
}
public static void main(String[] args) { Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
int[] arr = new int[n];
for(int i = 0;i < n;i++){
ar[i] = in.nextInt();
arr[i]= ar[i];
}
mergeSort(arr);
quickSort(ar, 0, ar.length-1);
System.out.println(answer - swaps);
}
}

【HackerRank】Running Time of Quicksort的更多相关文章

  1. 【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

    QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-array ...

  2. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  3. 【hadoop】 running beyond virtual memory错误原因及解决办法

    问题描述: 在hadoop中运行应用,出现了running beyond virtual memory错误.提示如下: Container [pid=28920,containerID=contain ...

  4. 【HackerRank】Find the Median(Partition找到数组中位数)

    In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific informati ...

  5. 【HackerRank】 The Full Counting Sort

    In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...

  6. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  7. 【POJ3784】Running Median

    Running Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3406   Accepted: 1576 De ...

  8. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  9. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

随机推荐

  1. Consul实现原理系列文章3: Consul的整体架构

    工作中用到了Consul来做服务发现,之后一段时间里,我会陆续发一些文章来讲述Consul实现原理.在前几篇文章介绍完了Consul用到的两个关键性东西Raft和Gossip之后,这篇文章会讲述Con ...

  2. es6中顶层对象属性≠全局属性

    先思考一下下面代码的输出结果是什么 const a = { x:1, fn:()=>this.x+=1 } const x = 1 a.fn() console.log(a.x,x) 正确答案为 ...

  3. HTML5实现端访问时禁止放大和缩小网页

    <title>html5禁止和移动.缩放网页</title><meta name="viewport" content="width=dev ...

  4. python 使用正则表达式的爬虫

    下面我们一起尝试一下爬取内涵段子网站: http://www.neihan8.com/article/list_5_1.html 打开之后,不难看到里面一个一个灰常有内涵的段子,当你进行翻页的时候,注 ...

  5. 使用Salt-ssh部署Salt-minion之yum安装(一)

    一.RPM安装篇 使用salt-ssh功能要求客户端系统python版本2.6+ 1.salt-minion端环境: 系统:CentOS 6.5 64bit python版本:2.6.6 2.配置/e ...

  6. linux c 常常混淆的概念

    指针函数 and 函数指针 指针函数是指带指针的函数,即本质是一个函数.函数都有返回类型(假设不返回值,则为无值型),仅仅只是指针函数返回类型是某一类型的指针. 定义格式 类型名 *函数名(函数參数列 ...

  7. ChemDraw能够生成化学性质报告吗

    ChemDraw破解版是一种不安全的软件版本,危害用户信息安全,一些不法分子正是因利用用户寻求免费软件的心理以ChemDraw破解版.ChemDraw注册机为噱头传播不安全信息.如果使用者已经成功安装 ...

  8. linux下的shell 快捷键

    Ctrl+p重复上一次命令Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置Ctrl+b前移一个字符不删除字符情况下Ctrl+h删除前一个字符Ctrl+u删除提示符前的所有字符Ct ...

  9. Laravel创建Route

    <?php /* |-------------------------------------------------------------------------- | Routes Fil ...

  10. RabbitMQ中Queue详细介绍

    新建队列 新建Queue时有很多参数,都代表什么含义,在这里解释一下: 前述:Rabbit版本为3.7.6 ErLang 版本为 21.0.1 Name 必填项,队列的名字,建议格式可以为多个字段,表 ...