[Interview Questions] sorting of K-order Arrays

Source: Internet
Author: User

I saw an interview question on the Internet and thought it was a good idea. I 'd like to add it to my favorites.

What is a K-order array first?

Arrays with such features:

The I-th element is located between [I-K, I + k] After sorting.

K-order

Requirement: The test algorithm sorts a K-order array.

 

Solution:

According to the K-order array feature, there are obviously the following subsequences:

X [0], X [k + 1], X [2 (k + 1)], X [3 (k + 1)] ......

X [1], X [k + 1 + 1], X [2 (k + 1) + 1], X [3 (k + 1) + 1] ......

........................

X [K], X [k + 1 + K], X [2 (k + 1) + K], X [3 (k + 1) + k] ......

This k + 1 sub-sequence has been sorted, and the rest of the task isMerge

There are two methods:

Method 1: For a normal merge, the complexity is O (NK), which is the same as merging two arrays)

Method 2: Use the loser tree to merge the time when an element requires a logk and the time when n elements require O (nlogk ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.