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 8885453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:18:53+00:00 2026-06-14T21:18:53+00:00

I have some tasks about sorting arrays in C#. I’ve been trying everything I

  • 0

I have some tasks about sorting arrays in C#. I’ve been trying everything I could think of – no luck.

The task is to sort an array of integers by known sorting algorithms (insertion, selection, bubble, quick). Thing is, I have to sort ONLY the smallest M elements.

Example: I have an array of 7 elements 2 9 8 3 4 15 11 and I need to sort the smallest 3 elements so that my array becomes 2 3 4 9 8 15 11.

Please help, I can’t seem to find anything neither here in SO, nor anywhere through Google. I don’t ask to do all the algorithms for me, I just need one of those just to get hold on how’s that possible.

E: Thank you for your thoughts. I’ve reviewed all of your recommendations and have accomplished to make an insertion sort like that:

static int[] insertSort(int[] arr, out int swaps, out int checks) {
    int step = 0;
    swaps = 0;
    checks = 0;
    for (int i = 0; i < arr.Length; i++) {
        int min = arr[i], minind = i;
        for (int j = i + 1; j < arr.Length; j++) {
            checks++;
            if (arr[j] < min) {
                min = arr[j];
                minind = j;
            }
        }
        int temp = arr[minind];
        if (step < M) {
            for (int j = minind; j > i; j--) {
                swaps++;
                arr[j] = arr[j - 1];
            }
            arr[i] = temp;
            swaps++;
            step++;
        }
    }
    return arr;
}

Swaps and checks – requirement for my application.

P.S. I’ve seen many times that SO doesn’t like to do homework for someone. That’s why I haven’t asked for code, I’ve just asked for thoughts on how to accomplish that.

Thanks again for those who have helped me out here.

  • 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-06-14T21:18:54+00:00Added an answer on June 14, 2026 at 9:18 pm

    Since there is no efficiency limitations:

    1. Set i to 0.
    2. Look for the minimum among the not sorted elements.
    3. Insert it into the position i, shift the array.
    4. Increment i.
    5. Repeat M times.

    Complexity is O(N * M).

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

Sidebar

Related Questions

Let's assume we have some task, that could be divided into independent subtasks and
everyone I have some question about tasks in Linux, I know that all tasks
Have some employee segmentation tasks that result in a large number of records (about
I have some task in my script that is about converting video with ffmpeg
I have 3 columns of data. Column B holds some data about tasks, column
I have some rake tasks that run each minute and of course that spawns
I have an android application in which I have to execute some tasks at
I have some task regarding do operations on database using ADODB in Vb.NET. Can
I have some async method public static Task<JObject> GetUser(NameValueCollection parameters) { return CallMethodApi(users.get, parameters,
I have some basic idea on how to do this task, but I'm not

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.