Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:30:24+00:00 2026-05-27T03:30:24+00:00

I invented new sort algorithm. Well, I understand that I can’t invent something good,

  • 0

I “invented” “new” sort algorithm. Well, I understand that I can’t invent something good, so I tried to search it on wikipedia, but all sort algorithms seems like not my. So I have three questions:

  1. What is name of this algorithm?
  2. Why it sucks? (best, average and worst time complexity)
  3. Can I make it more better still using this idea?

So, idea of my algorithm: if we have an array, we can count number of sorted elements and if this number is less that half of length we can reverse array to make it more sorted. And after that we can sort first half and second half of array. In best case, we need only O(n) – if array is totally sorted in good or bad direction. I have some problems with evaluation of average and worst time complexity.

Code on C#:

    public static void Reverse(int[] array, int begin, int end) {
        int length = end - begin;
        for (int i = 0; i < length / 2; i++)
            Algorithms.Swap(ref array[begin+i], ref array[begin + length - i - 1]);
    }
    public static bool ReverseIf(int[] array, int begin, int end) {
        int countSorted = 1;
        for (int i = begin + 1; i < end; i++)
            if (array[i - 1] <= array[i])
                countSorted++;

        int length = end - begin;
        if (countSorted <= length/2)
            Reverse(array, begin, end);

        if (countSorted == 1 || countSorted == (end - begin))
            return true;
        else
            return false;
    }
    public static void ReverseSort(int[] array, int begin, int end) {
        if (begin == end || begin == end + 1)
            return;
        // if we use if-operator (not while), then array {2,3,1} transforms in array {2,1,3} and algorithm stop
        while (!ReverseIf(array, begin, end)) {
            int pivot = begin + (end - begin) / 2;
            ReverseSort(array, begin, pivot + 1);
            ReverseSort(array, pivot, end);
        }
    }
    public static void ReverseSort(int[] array) {
        ReverseSort(array, 0, array.Length);
    }

P.S.: Sorry for my English.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:30:25+00:00Added an answer on May 27, 2026 at 3:30 am

    The best case is Theta(n), for, e.g., a sorted array. The worst case is Theta(n^2 log n).

    Upper bound

    Secondary subproblems have a sorted array preceded or succeeded by an arbitrary element. These are O(n log n). If preceded, we do O(n) work, solve a secondary subproblem on the first half and then on the second half, and then do O(n) more work – O(n log n). If succeeded, do O(n) work, sort the already sorted first half (O(n)), solve a secondary subproblem on the second half, do O(n) work, solve a secondary subproblem on the first half, sort the already sorted second half (O(n)), do O(n) work – O(n log n).

    Now, in the general case, we solve two primary subproblems on the two halves and then slowly exchange elements over the pivot using secondary invocations. There are O(n) exchanges necessary, so a straightforward application of the Master Theorem yields a bound of O(n^2 log n).

    Lower bound

    For k >= 3, we construct an array A(k) of size 2^k recursively using the above analysis as a guide. The bad cases are the arrays [2^k + 1] + A(k).

    Let A(3) = [1, …, 8]. This sorted base case keeps Reverse from being called.

    For k > 3, let A(k) = [2^(k-1) + A(k-1)[1], …, 2^(k-1) + A(k-1)[2^(k-1)]] + A(k-1). Note that the primary subproblems of [2^k + 1] + A(k) are equivalent to [2^(k-1) + 1] + A(k-1).

    After the primary recursive invocations, the array is [2^(k-1) + 1, …, 2^k, 1, …, 2^(k-1), 2^k + 1]. There are Omega(2^k) elements that have to move Omega(2^k) positions, and each of the secondary invocations that moves an element so far has O(1) sorted subproblems and thus is Omega(n log n).


    Clearly more coffee is required – the primary subproblems don’t matter. This makes it not too bad to analyze the average case, which is Theta(n^2 log n) as well.

    With constant probability, the first half of the array contains at least half of the least quartile and at least half of the greatest quartile. In this case, regardless of whether Reverse happens, there are Omega(n) elements that have to move Omega(n) positions via secondary invocations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to generate a new image name for each image that is
I often want to write something like this: new Form { Text = Caption,
I'm new at MVC and can't get this to work. I basically have a
I'm fairly new to Apache Camel, but have to say that I love it
I'm working on a new web site than can generate company/product names. Someone can
[TestInitialize()] public void MyTestInitialize() { XmlTextWriter writer = new XmlTextWriter(DataFile.xml, Encoding.UTF8); writer.Formatting = Formatting.Indented;
I heard about B-Method which is invented in France. Is it an alternative to
No, wait. I'm being totally serious. When HTTP was invented, FTP already existed. Why
I have a string that represents a non indented XML that I would like
Can I use Html Agility Pack to make the output look nicely indented, unnecessary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.