Advertisement
Guest User

4chan number sorting

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