Advertisement
Guest User

4chan number sorting 3

a guest
Jun 24th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // list      - list of integers
  4. // size      - size of the list
  5. // target   - number to put at the end
  6. void move_to_back(int *list, size_t size, int target) {
  7.     unsigned int index = 0;
  8.     unsigned int index_target = 0;
  9.     int solved_list[size]; 
  10.  
  11.     // Sort array
  12.     for (unsigned int i = 0; i < size; i++) {
  13.         if (list[i] != target) {
  14.             solved_list[index++] = list[i];
  15.         } else {
  16.             solved_list[size-1 - index_target++] = target;
  17.         }
  18.     }
  19.    
  20.     // Copy solved_list to list (Do I need a free() here?)
  21.     memcpy(list, solved_list, sizeof(solved_list));
  22. }
  23.  
  24.  
  25. int main() {
  26.     int list[9] = {1,9,7,4,9,3,2,6,9};
  27.     move_to_back(list, 9, 9);
  28.    
  29.     for (int i = 0; i < 9; i++)
  30.         printf("%d ", list[i]);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement