PAT Advanced 1101 Quick Sort

Overview

I will show linear time/space algorithm to solve PAT advanced 1101 Quick Sort by using min[i] to keep minimum among input[i…length), with test cases.

PAT Advenced 1101 Quick Sort Problem

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

For example, given N = 5 and the numbers 1, 3, 2, 4, and 5. We have:

  • 1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
  • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
  • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
  • and for the similar reason, 4 and 5 could also be the pivot. Hence in total there are 3 pivot candidates.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then the next line contains N distinct positive integers no larger than 109. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

5
1 3 2 4 5

Sample Output:

3
1 4 5

PAT Advenced 1101 Quick Sort Algorithm

The brute force way is simple, for earch of the input, we scan its left and right portions to determine if it is less than any number from the right portion, and larger than any number from the left portion, if so, it is a pivot, if not, it is not. For every number we need O(N) time to scan, so the total time would be O(N^2).

The brute force is not efficient, if we whink further, we know, in order to know input[i] is less than any number from right part is equivalent to compare input[i] with the minimum from the right part, and similarly, we only need to compare input[i] with the maximum from left part, this comparition takes O(1) time, and we know we can find all such max and min by one pass scan, which takes O(N) time, so the total time complexity is O(N), the following source code is accepted by PAT OJ to pass this 1101 Quick Sort problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

int main() {
    std::vector<int> vals;
    std::vector<int> pivots;
    std::vector<int> mins;

    int n = 0;
    std::cin >> n;
    vals.resize(n);
    mins.resize(n + 1);

    for (int i = 0; i < n; ++i) {
        std::cin >> vals[i];
    }

    int max = vals[0];
    mins[n-1] = vals[n-1];
    mins[n] = INT_MAX;

    for (int i = n - 2; i > 0; --i) {
        mins[i] = std::min(mins[i+1], vals[i]);
    }

    for (int i = 0; i < n; ++i) {
        if (vals[i] < mins[i + 1] && vals[i] >= max) {
            pivots.push_back(vals[i]);
        }
        max = std::max(max, vals[i]);
    }

    std::sort(pivots.begin(), pivots.end());
    bool init = true;
    std::cout << pivots.size() << std::endl;

    for (auto &e : pivots) {
        if (init) {
            std::cout << e;
            init = false;
        } else {
            std::cout << ' ' << e;
        }
    }
    std::cout << std::endl;

    return 0;
}

PAT Advenced 1101 Quick Sort Test Cases

Be careful about the case where there is no pivot number at all, in this case, just output zero without a second line.

Summary

I discussed the linear time/space algorithm to solve PAT advanced 1101 Quick Sort by using min[i] to keep minimum among input[i…length), with test cases.

Written on February 1, 2016