title: Sort the array in decreasing order of frequency of occurrence of elements in C
body: <p>Question is to sort the array according to the frequency of the elements. For example, if the input array is </p>
  
  <pre><code>   { 2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12 }
  </code></pre>
  
  <p>then modify the array to:</p>
  
  <pre><code>   { 3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5 }
  </code></pre>
  
  <p>I wrote the code for this and it is working correctly, but it is using a lot of space and has very high complexity.</p>
  
  <p>I am not satisfied with this solution and the logic I applied for this. Can anyone help to optimize this code or provide a better logic?</p>
  
  <p>My Code is:  </p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS // this line to work code in visual studio
  #include &lt;stdio.h&gt;
  
  int main() {
      /*
       * n = number of integer
       * i = loop variable
       * j = inner loop variable
       * c = number of distinct input
       * buf = temprary storage for input value
       * k = possibility of frequency of any no.
       */
  
      int n, i, j, c = 0, buf, k;
      int b; //act as flag
      int arr[100] = { 0 };
      int stack[200] = { 0 };
      int top = -1;
      printf("Enter the size of array(integer between 1-100):");
      scanf("%d", &amp;n);
      n *= 2;
  
      printf("----------Enter the elements in the array----------\n\n");
  
      for (i = 0; i &lt; n; i += 2) {
          b = 0;
          printf("Enter the element:");
          scanf("%d", &amp;buf);
          for (j = 0; j &lt;= i; j += 2) {
              if (arr[j] == buf) {
                  arr[j + 1]++;
                  b = 1;
              }       
          }
          if (b == 0) {
              c++;
              arr[c * 2 - 2] = buf;
              arr[c * 2 - 1]++;
          }
      }
  
      for (i = 0; i &lt; c * 2; i++)
          printf("%d ", arr[i]);
  
      //input done in form of (element,times of occurence i.e. frequency),to print array, write this outside of comment: 
      //for (i = 0; i &lt; c * 2; i++) printf("%d ", arr[i]);
  
      for (k = 1; k &lt; n / 2; k++) {   //checking for possible frequencies
          for (j = c * 2 - 1; j &gt; 0; j -= 2) {
              //locations(index) to check in array for frequency
              //left to right, so with same frequency no.,which occurred first will push in last.
              if (arr[j] == k)
                  stack[++top] = j; //pushing(index of frequency) into stack in increasing order of frequency     
          }
      }
  
      //to print stack, write this outside of comment:
      //printf("\nstack\n");
      //for (i = top; i &gt; -1; i--) printf("%d ",stack[i]);
  
      //printing of elements in there decreasing order of frequency(pop from stack)
      //we have to print element, number of times of its frequency
  
      printf("\n\n----------Output array in sorted order of there frequency----------\n");
      for (top; top &gt; -1; top--) {        
          for (j = arr[stack[top]]; j &gt; 0; j--)
              printf("%d ", arr[stack[top] - 1]);
      }
  }
  </code></pre>