Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that you cannot use <code>qsort()</code> because one of your constraints is to write your own sorting algorithm, here goes.</p> <p>What do you need to sort? An array of<code>job</code>. </p> <p>What is the key by which you need to sort? <code>job.prio</code>.</p> <p>So how to sort? Any common selection, insertion, or (eek) bubble sort will do. (Though if you go down the bubble sort route, at least make it sexy and do a cocktail shaker sort.) Just compare two <code>job.prio</code>s, both are <code>int</code>s so that's not hard, and swap the places of their respective <code>job</code> structures in the array as needed. </p> <p>Here is a selection sort algorithm that will work. You can find plenty of others on Google. </p> <pre><code>void selectionSort (job* jobs, int size) { int smallest; job temp; for (int i = 0; i &lt; size - 1; i++) { smallest = i; for (int walk = i + 1; walk &lt;= size - 1; walk++) { if (jobs[walk].prio &lt; jobs[smallest].prio) smallest = walk; } // end inner loop temp = jobs[i]; jobs[i] = jobs[smallest]; jobs[smallest] = temp; } // end outer loop return; } </code></pre> <p>Fairly simple; it's just like any old selection sort. But selection sort is boring. Try to do an insertion sort, now that the selection sort gives you the general idea of how to swap the elements in the array. </p> <p>Note that there are other problems with your code, as folks have pointed out: you are allocating space for only one job in your array, but you need eight; you have undefined variables like <code>eerste</code>; you have <code>name</code> declared twice, once as a <code>char*</code> and once as an array of <code>char</code>. So there's plenty of cleanup, but hopefully you have enough idea now to be able to finish your assignment. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload