#Array sorting algorithm issue

21 messages · Page 1 of 1 (latest)

light briar
#
void sorting(int* arr, int size, bool order = false)
{
    if (!order)
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 1; j < size; j++) {
                if (arr[i] < arr[j]) 
                    if(arr[i] != arr[j])
                       arr[i] = arr[j];
            }
        }
    }

so why does this spam the biggest number instead of acc organizing them
i think the algorithm should work fine. what am i missing ?
it should organize it descending

ancient owlBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

meager umbra
#

arr[i] = arr[j]

#

This does not swap the two elements

light briar
#

oh

#

so i need to create a temp var

#

to store the arr[i] in

meager umbra
#

This overwrites the ith index woth the value of he jth index

light briar
#

and then equal it with arr[j]

#

it still doesnt work

#

that's the output now

meager umbra
#

Show us your updated code

light briar
#

and in the debugger it seems ifne

#
if (!order)
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 1; j < size; j++) {
                if (arr[i] < arr[j]) 
                {
                    if (arr[i] != arr[j]) 
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
#

just added the temp var

meager umbra
#

What if j also started at 0

light briar
#

does nothing

#

i almost fixed it but there is 1 problem

#
if (!order)
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = size ; j >=0; j--) {
                if (arr[i] < arr[j]) 
                {
                    if (arr[i] != arr[j]) 
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }
    }
}
#

!solved