Selection sort in C modified not working for all cases

0 votes

I was attempting to tweak the selection sort code in C++ in order to test the results. 

My updated code is as follows:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cout<<"Enter number of elements\n";
    cin>>n;
    int i,j,small,pos,t;
    int a[n];
    cout<<"Enter elements of array\n";
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-1;i++)
    {
        small=a[i];
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<small)
            {
                small=a[j];
                pos=j;
            }
            t=a[i];
            a[i]=a[pos];
            a[pos]=t;
        }
    }
    cout<<"Sorted array:\n";
    for(i=0;i<n;i++)
    cout<<a[i]<<" ";
    return 0;
}

This code is valid for certain arrays but not for others. 

As an example: 

When I input 1,11,2,22,3 as the array, I receive the correct result: 1,2,3,11,22. 

However, if I enter the array as 1,11,2,22,3,33, I receive the same array as the input as the result. 

Please let me know what's wrong with my code.

Jun 29, 2022 in C++ by Damon
• 4,960 points
507 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
0 answers

Code Not working in VS Code but works in OnlineGDB

I am practicing about primeSieve with C++ language in VS Code 1.57.1. Can ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
730 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
699 views
0 votes
1 answer

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
690 views
0 votes
1 answer

Is 'If Else' statement indentation important or not in C++? [duplicate]

White space has no effect on the understanding of code in C and C++.  That is not to say that the programmer should be unconcerned about its misuse. The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below.  The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets. This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning. while(c != cols) { ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
567 views
0 votes
0 answers

Difference between for loop and the range based loop in C++

The distinction between a for loop and ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
600 views
0 votes
0 answers

C++ vector erase function not working properly

Here is the code I want to use to delete any entries with values more than 2 and fewer than 5. vector<int> myvector{3, 3, 3, 3, 3, 3, ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
621 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,283 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,157 views
0 votes
1 answer

implementing merge sort in C++

To respond to the question:  std::vectorT> is used to create dynamically sized arrays at runtime.  Ideally, you'd use one of these to get feedback.  If not, they are simple to convert.  For instance, you might make two arrays like this: template <typename T> void merge_sort(std::vector<T>& array) { ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
891 views
0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
467 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP