title: Trying to sort a struct array in Descending Order
body: <p>To preface what I'm trying to do - I'm trying to make a Shortest Job First (SJF), the OS algo in C.</p>
  <p>And to handle the sort-by-burst-time, I'm pushing all the ready processes in a stack, and then sorting that stack in a descending order. So for example - if the stack looks like 3,5,1,2 with &quot;2&quot; on top, I'm trying to sort it as: 5,3,2,1 with &quot;1&quot; on top. So when I start using pop, I'll get elements in the order I require.</p>
  <p>But the problem with the code I'm going to show is, it orders 1,1,5,3 as 5,1,1,3. The last process &quot;3&quot; is never sorted.</p>
  <p>Code for Sorting:</p>
  <pre><code>void sortByBurst() {
      int size = top;
      struct fcfs temp;
      for (int i = 0; i &lt; size - 1; i++) {
          for (int j = 0; j &lt; (size - 1 - i); j++) {
              if (stack[j].burst &lt; stack[j + 1].burst) {
                  temp = stack[j];
                  stack[j] = stack[j + 1];
                  stack[j + 1] = temp;
              }
          }
      }
  }
  </code></pre>
  <p>And in case you want to see where I'm handling the rest of the logic i.e making decisions on what to push - this is the for loop for that:</p>
  <pre><code>for (i = 0; i &lt; numOfProc; i++) {
          if (first_process == 1 &amp;&amp; p[i].arrival &lt;= current_time &amp;&amp; p[i].completed == 0) {
              min_index = i;
          } else if (first_process == 0 &amp;&amp; last_index != -1) {
              for (int k = 0; k &lt; numOfProc; k++) {
                  if (p[k].completed == 0 &amp;&amp; p[k].arrival &lt;= p[last_index].max_slot &amp;&amp; p[k].inQ != 1) {
                      push(p[k]);
  
                      p[k].inQ = 1;
                      critical_case = 1;
                  } else if (p[k].completed == 0 &amp;&amp; p[k].arrival &gt; finalp[last_index].max_slot &amp;&amp; p[k].inQ != 1) {
                      min_index = k;
                  }
              }
  
              if (critical_case == 1) {
                  sortByBurst();
                  struct fcfs compare = pop();
                  for (int l = 0; l &lt; numOfProc; l++) {
                      if (compare.pid == p[l].pid) {
                          min_index = l;
                          break;
                      }
                  }
              }
              break;
          }
  
      }
  </code></pre>
  <p>The input I'm testing my code with is:</p>
  <pre><code> 1. Process 1 | 2 (Arrival) | 1 (Burst)
   2. Process 2 | 1 (Arrival) | 5 (Burst)
   3. Process 3 | 4 (Arrival) | 1 (Burst)
   4. Process 4 | 0 (Arrival) | 6 (Burst)
   5. Process 5 | 2 (Arrival) | 3 (Burst)
  </code></pre>
  <p>Required Output:</p>
  <pre><code> 1. Process 4 | 0 (Arrival) | 6 (Burst)
   2. Process 1 | 2 (Arrival) | 1 (Burst)
   3. Process 3 | 4 (Arrival) | 1 (Burst)
   4. Process 5 | 2 (Arrival) | 3 (Burst)
   5. Process 2 | 1 (Arrival) | 5 (Burst)
  </code></pre>
  <p>Output I'm Getting:</p>
  <pre><code> 1. Process 4 | 0 (Arrival) | 6 (Burst)
   2. Process 5 | 2 (Arrival) | 3 (Burst)
   3. Process 3 | 4 (Arrival) | 1 (Burst)
   4. Process 1 | 2 (Arrival) | 1 (Burst)
   5. Process 2 | 1 (Arrival) | 5 (Burst)
  </code></pre>
  <p>As you can see, the Process 5 should be at 4th place in the list. But because in Stack - it's still on Top even after sorting, the algo is not working as it should. Kindly look into what I'm doing wrong here.</p>
  <p><strong>Complete Code:</strong></p>
  <pre><code>    #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct fcfs {
      int pid, arrival, burst, min_slot, max_slot, wait_time, completed, inQ;
  };
  
  struct fcfs stack[10];
  int top = -1;
  
  void push(struct fcfs process);
  
  struct fcfs pop();
  
  void sortByBurst();
  
  void pline(int x);
  
  void main() {
  
      int i, numOfProc, j;
      int counter = 0;
      int current_time = 0;
      int completed = 0;
      struct fcfs p[10], finalp[10];
      int first_process = 1;
      int prevMaxSlot = 0;
      int last_index = -1;
      int critical_case = 0;
  
      printf(&quot;Enter total number of Processes \n&quot;);
      scanf(&quot;%d&quot;, &amp;numOfProc);
  
      for (i = 0; i &lt; numOfProc; i++) {
          printf(&quot;Enter Arrival Time &amp; Burst Time for Process %d: \n&quot;, i + 1);
          scanf(&quot;%d %d&quot;, &amp;p[i].arrival, &amp;p[i].burst);
          p[i].pid = i + 1;
          p[i].wait_time = 0;
          p[i].completed = 0;
          p[i].inQ = 0;
      }
  
      int prev = 0;
  
      while (completed != numOfProc) {
  
          int min_index = -1;
          for (i = 0; i &lt; numOfProc; i++) {
              if (first_process == 1 &amp;&amp; p[i].arrival &lt;= current_time &amp;&amp; p[i].completed == 0) {
                  min_index = i;
              } else if (first_process == 0 &amp;&amp; last_index != -1) {
                  for (int k = 0; k &lt; numOfProc; k++) {
                      if (p[k].completed == 0 &amp;&amp; p[k].arrival &lt;= p[last_index].max_slot &amp;&amp; p[k].inQ != 1) {
                          push(p[k]);
  
                          p[k].inQ = 1;
                          critical_case = 1;
                      } else if (p[k].completed == 0 &amp;&amp; p[k].arrival &gt; finalp[last_index].max_slot &amp;&amp; p[k].inQ != 1) {
                          min_index = k;
                      }
                  }
  
                  if (critical_case == 1) {
                      sortByBurst();
                      struct fcfs compare = pop();
                      for (int l = 0; l &lt; numOfProc; l++) {
                          if (compare.pid == p[l].pid) {
                              min_index = l;
                              break;
                          }
                      }
                  }
                  break;
              }
  
          }
  
          if (min_index == -1) {
              current_time++;
          } else {
              if (p[min_index].arrival == prevMaxSlot) {
                  p[min_index].min_slot = prevMaxSlot;
                  p[min_index].max_slot = p[min_index].arrival + p[min_index].burst;
              } else if (p[min_index].arrival &lt; prevMaxSlot || p[min_index].arrival == prevMaxSlot) {
                  p[min_index].min_slot = prevMaxSlot;
                  p[min_index].max_slot = p[min_index].min_slot + p[min_index].burst;
              } else if (p[min_index].arrival &gt; prevMaxSlot) {
                  p[min_index].min_slot = p[min_index].arrival;
                  p[min_index].max_slot = p[min_index].arrival + p[min_index].burst;
              }
              p[min_index].wait_time = (first_process == 1) ? 0 : abs(p[i].min_slot - prev);
              p[min_index].completed = 1;
              prev = current_time;
              prevMaxSlot = p[min_index].max_slot;
  
  
              finalp[counter++] = p[min_index];
              first_process = 0;
              last_index = min_index;
              completed++;
  
  
          }
  
      }
  
  
      pline(44);
      printf(&quot;Slot\tPID\tArrival\t\tBurst\n&quot;);
      pline(44);
  
  
      for (i = 0; i &lt; numOfProc; i++) {
          if ((finalp[i].min_slot - finalp[i - 1].max_slot) &gt; 0 &amp;&amp; i &gt; 0) {
              printf(&quot;%d - %d\tNONE\tNONE\t\tNONE\n&quot;, finalp[i - 1].max_slot, finalp[i].min_slot);
          } else if ((abs(0 - finalp[i].min_slot) &gt; 0 &amp;&amp; i == 0)) {
              printf(&quot;0 - %d\tNONE\tNONE\t\tNONE\n&quot;, finalp[i].min_slot);
          }
  
          printf(&quot;%d - %d\t%d\t%d\t\t%d\n&quot;, finalp[i].min_slot, finalp[i].max_slot, finalp[i].pid, finalp[i].arrival,
                 finalp[i].burst);
      }
      pline(44);
  
  }
  
  
  void pline(int x) {
      for (int i = 0; i &lt; x; i++) {
          printf(&quot;-&quot;);
      }
      printf(&quot;\n&quot;);
  }
  
  void push(struct fcfs process) {
      if (top == 10)
          printf(&quot;\n Overflow&quot;);
      else {
          top = top + 1;
          stack[top] = process;
      }
  }
  
  
  struct fcfs pop() {
      struct fcfs process;
      if (top == -1)
          printf(&quot;Underflow&quot;);
      else {
          process = stack[top];
          top = top - 1;
      }
  
      return process;
  
  }
  
  void sortByBurst() {
      int size = top + 1;
      struct fcfs temp;
      for (int i = 1; i &lt; size - 1; i++) {
          for (int j = 0; j &lt; (size - 1 - i); j++) {
              if (stack[j].burst &lt; stack[j + 1].burst) {
                  temp = stack[j];
                  stack[j] = stack[j + 1];
                  stack[j + 1] = temp;
              }
  
          }
      }
  }
  </code></pre>