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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:33:16+00:00 2026-06-01T11:33:16+00:00

Ive written the following method which sorts an array by copying the biggest values

  • 0

Ive written the following method which sorts an array by copying the biggest values into another array. I would like to see alternatives to this approach. for example an approach which swaps the values in the primary array itself, thereby eliminating the need for a secondary array to copy the values to.

I do not want to use pre written .net library methods such as Array.sort or etc as my main goal is only to practice in writing algorithms.

also if anyone can tell me the weak points of the below code and its shortcomings and how it can be improved, it would be greatly appreciated.

thank you

private static void sortArray(int[] array)
{
    int[] sorted = new int[array.Length];
    int curMax = 0;      
    int bigIndex = 0;  

    for (int i = 0; i < array.Length; i++)
    {
        for (int j = 0; j < array.Length; j++)
        {
            if (array[j] > curMax)
            {
                bigIndex = j;
                curMax = array[j];
            }
        }

        sorted[i] = array[bigIndex];
        array[bigIndex] = 0;
        curMax = 0;
    }
}

example of bubble sort:

 private static void sortArray(int[] array)
        {

            bool lastExchange;
            do
            {

                lastExchange = false;

                for (int i = 1; i < array.Length; i++)
                {

                    if (array[i - 1] > array[i])
                    {
                        lastExchange = true;
                        int temp = array[i - 1];
                        array[i - 1] = array[i];
                        array[i] = temp;
                    }
                }
            } while (lastExchange);

        }
  • 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-01T11:33:18+00:00Added an answer on June 1, 2026 at 11:33 am

    You arlgorithm is (excuse me for saying it), pretty much as inefficient as a sorting algorithm can be. You could for example make it more efficient by simply swapping items in the array instead of leaving unused values in the array. By doing that you will reduce the number of items that you have to look through for each iteration:

    private static void sortArray(int[] array) {
      for (int i = 0; i < array.Length; i++) {
        int largest = array[i];
        int largeIndex = i;
        for (int j = i + 1; j < array.Length; j++) {
          if (array[j] > largest) {
            largeIndex = j;
            largest = array[j];
          }
        }
        array[largeIndex] = array[i];
        array[i] = largest;
      }
    }
    

    (Another problem with your algorithm is that it doesn’t work with negative values, or zero values.)

    One of the simplest sorting algorithms is bubble sort:

    private static void sortArray(int[] array) {
      bool cont = true;
      while (cont) {
        cont = false;
        for (int i = 1; i < array.Length; i++) {
          if (array[i - 1] > array[i]) {
            cont = true;
            int temp = array[i - 1];
            array[i - 1] = array[i];
            array[i] = temp;
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a method in Haskell which I would think is very
A unit test I've written is failing with the following error message. This method
I've written the following code to create a three-column layout where the first and
I've written the following code to (successfully) connect to a socks5 proxy. I send
I've written the following IComparer but I need some help. I'm trying to sort
I've written lots of Excel macros in the past using the following development process:
I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule
Ive written a Word addin in VS 2008 thats pretty simple, just adds a
I'm attempting to call a method written in C++/CLI from C#. The C++/CLI code
I know there’s been a good deal written on thumbnail generation and the like

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.