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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:54:38+00:00 2026-06-02T10:54:38+00:00

I am trying to make a simple program in C to implement Selection Sort

  • 0

I am trying to make a simple program in C to implement Selection Sort.

Below is my program:

#include<stdio.h>
#include<conio.h>

int main(void)
{
  int min, total, *arr,i,j,temp;

  clrscr();
  printf("Enter the size of array : ");
  scanf("%d",&total);
  arr = (int *) malloc(total * sizeof(int));

  for(i = 0; i<total; i++)
  {
    printf("\nEnter element %d: ", i+1);
    scanf("%d", &arr[i]);
  }

  min = arr[0];
  for(i = 0; i<total; i++)
  {
    for(j = i; j<total; j++)
    {
      if(arr[j]<min)
      { min=arr[j]; }
    }

    min = temp;
    min = arr[i];
    arr[i] = min;
  }

  for(i = 0; i<total; i++)
  {
    printf("%d \t", arr[i]);
  }

  getch();
  return 0;
}

Output:

enter image description here

The answer should have been

10 20 30 40 70 80

How can I fix my program?

EDIT 1:

Fixed Temp Issue after reading cnicutar’s answer

  min = arr[0];
  for(i = 0; i<total; i++)
  {
    for(j = i; j<total; j++)
    {
      if(arr[j]<min)
      { 
    min=arr[j]; 
    min_index = j;
      }
    }

       temp = a[i];
       a[i] = min;
       a[min_index] = temp;
  }

Now, I get this for the above input values: 10 10 10 20 10 10

EDIT 2:

Found the problem, the program was using the same minimum value for all Iterations

#include<stdio.h>
#include<conio.h>

int main(void)
{
  int min, min_index, total, *arr,i,j,temp;

  clrscr();
  printf("Enter the size of array : ");
  scanf("%d",&total);
  arr = (int *) malloc(total * sizeof(int));

  for(i = 0; i<total; i++)
  {
    printf("\nEnter element %d: ", i+1);
    scanf("%d", &arr[i]);
  }

  min = arr[0];               //           min = arr[0] = 70              ||   min = 10     <-- Problem          
  for(i = 0; i<total-1; i++)  //           i = 0                          ||                 
  {                           //                                          ||
    for(j = i; j<total; j++)  //           j = i = 0                      ||                
    {                         //                                          ||                          
      if(arr[j]<min)          //           30<70     ; 40<30 ; 10<30      ||                                   
      {                       //                                          ||
    min=arr[j];               //           min = 30  ;       ; min = 10   ||                                            
    min_index = j;            //           index = 1 ;       ; index = 3  ||                                                                    
      }                       //                                          ||
    }                         //                                          ||
                              //                                          ||
    if(min!=arr[i])           //                                          ||
    {                         //                                          ||
       temp = arr[i];         //                                          ||
       arr[i] = min;          //                                          ||
       arr[min_index] = temp; //     After swap:     10 30 40 70 80 20    ||
    }
    min = arr[i+1];           // (Add this to fix the problem)  
  }

  for(i = 0; i<total; i++)
  {
    printf("%d \t", arr[i]);
  }

  getch();
  return 0;
}
  • 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-02T10:54:43+00:00Added an answer on June 2, 2026 at 10:54 am

    At least 2 problems: your “swap” is wrong:

    min = temp;
    min = arr[i];
    arr[i] = min;
    

    And you should be remembering the index of the minimum element, changing min won’t do. Specifically: you want to change arr[mystery] where mystery = j in your if.

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

Sidebar

Related Questions

Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
I am trying to make a simple program with SFML. So far I have
I am trying to make a simple program that could open a new activity
I'm new to iPhone programming, and I'm trying to make a simple program without
I'm a beginner in Java, and NetBeans. I'm trying to make a simple program
I'm trying to make a simple encryption program with C. My aim is to
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I am trying to make a simple program to convert time given in seconds
i m trying to make a simple program ( & yes , it is

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.