Loop through an array and sort the array?

I need to loop through this array given a data file and fill it, but while doing so make sure it is sorted in acending order. My issue is that my code loops and fills, but doesn't sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void insertIntoSortedArray(int myArray[ ], int numEntries, int newValue)
{
  int n;
  int t;
  n = newValue;
  t = numEntries - 1;
    for(int i = 0; i <= numEntries; i++ )
    {
      myArray[t+1] = myArray[t];
      t = t-1;
      if(myArray[t] > n)
      {
        myArray[t+1] = myArray[n];
      }
    }
}
Last edited on
I see that your loop operates only on the array elements [t+1], [t] and [n].

Given that t and n are two fixed, unchanging values, it seems that your loops just operates on the same three elements of the array over and over.
Ok I should clarify. I have an array of size 100. There is a data file with 100 elements. We read in the file and fill the array. All of this works. However, what we are also supposed to do is create a function that sorts the array.

NumEntries is the array size or rather how many indeces are filled.

newValue is the next line in our data file that needs to be put into the array, but sorted as such in acending order.

Here is another form of what I was attempting to do also.

1
2
3
4
5
6
7
8
9
10
int temp;
  for(int i = 0; i < numEntries - 1; i++) {
    for(int j = i+1; j < numEntries; j++) {
      if(myArray[newValue[i]] > myArray[newValue[j]])
      {
        temp = newValue[i];
        newValue[i] = newValue[j];
        newValue[j] = temp;
      }
    }
Given that t and n are two fixed
t is decremented, but regardless the logic is flawed.
I believe he is attempt to start at the back of the array, and then shift the elements over until the value he is inserting is no longer greater than the existing element.

jlm, what does your array look like before you call this function? An array is a static size. Are you leaving enough room for it to grow?

The logic is flawed for probably a few reasons, but the major reason it you are using n as a value for comparison on line 11, but then you're using it as an index on line 13. You do not want to be using it as an index.

jlm, start small, and work out your logic by hand. Start with just 2 elements, e.g. [2, 5, ~, ~ ...] (where ~ is some dummy value, the logical size of array is still 2).

Since you are inserting in ascending order, I personally think it makes more sense to increment your counter variable, as opposed to decrementing like you are now.
1
2
3
4
5
6
7
8
9
10
11
int index_to_insert = numEntries;
for (int i = 0; i < numEntries; i++)
{
    if (myArray[i] > newValue)
    {
        index_to_insert = i;
    }
}

// perform the insertion at index_to_insert
// push over existing elements 

Last edited on
jlmccart01, you need to write a sorting algorithm. This will give you some help with that.

https://www.geeksforgeeks.org/sorting-algorithms/
Last edited on
Topic archived. No new replies allowed.