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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:11:45+00:00 2026-06-17T10:11:45+00:00

I am trying to code a selection sort algorithm (takes a random set of

  • 0

I am trying to code a selection sort algorithm (takes a random set of ten numbers, input by user, and outputs the list in ascending order). I understand how the algorithm is supposed to work, and thought I had figured it out, but something in the for-loop isn’t working the way that I was expecting and I can’t for the life of me figure out what it is.

Here is an example of the problem that I am encountering:
Input {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, outputs {0, 1, 2, 3, 4, 9, 6, 7, 8, 9}

The fifth index always seems to be off, no matter what numbers I put in. However, if the original number set has multiples of any given number, then there will be other errors that are harder to pinpoint. I am hoping that after solving the fifth index issue, the other issues will be solved or at least easier to pinpoint.

Below is then entirety of my main function (minus the part where it actually says main).

int arr[10];//array of integers input by user
int num;    //smallest number in array
int temp;   //temp variable for swapping numbers
int ind;    //index of where temp was found

cout << "Enter ten random integers: " << endl;
for(int i=0; i<10; i++)
{
    cout << "[" << i << "] = ";
    cin >> arr[i];
}

cout << endl;

for (int j=0; j<10; j++)
{
    num = arr[j];
    temp = arr[j];

    for (int k=j; k<10; k++) /*after this loop, temp should have lowest int and ind
                               should have its location*/
    {
        if(temp > arr[k])
        {
            temp = arr[k];
            ind = k;
        }
    }
    arr[j] = temp;
    arr[ind] = num;
}

for(int l = 0; l<10; l++)
{
    cout << arr[l] << " ";
}
  • 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-17T10:11:46+00:00Added an answer on June 17, 2026 at 10:11 am
    temp = arr[0]; // You need to set it to sth diferent then 0
    
    for(int i=0; i<10; i++)
    {
        if(arr[k] < temp)
        {
               temp = arr[k];
               ind = k;
        }
    }
    

    And done.

    For sorting (123):

      for (i = 10; i > 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    

    And reverse (321)

      for (i = 10; i > 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] < numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to change the current selection of a NSTableView with the following code:
I am trying to sort date-time strings using Regex in my javascript code but
I'm trying to write some code that checks whether blurred pictures that the user
I am trying to load options to drop-down list depending on the selection made
Hi I am trying to implement Selection Sort in vb.net using recursion. Problem is
I'm trying to create a simple(?) selection sort program in C that selects the
i'm trying to figure out the code for the jquery selector test found at
I'm trying to run a short section of my code that gets 2 ranges
I am having an issue converting type. I was trying code like this (minimal,
I´m trying to code a tooltip (Yes I know, I have my reasons to

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.