fork(39) download
  1. #include <stdio.h>
  2.  
  3. void dupes(int a[], int n)
  4. {
  5. int swaps = 0;
  6. int i;
  7.  
  8. for (i = 0; i < n; i++)
  9. while (a[a[i]] != a[i]) {
  10. int tmp = a[i];
  11. a[i] = a[tmp];
  12. a[tmp] = tmp;
  13. swaps++;
  14. }
  15.  
  16. for (i = 0; i < n; i++)
  17. if (a[i] != i)
  18. printf("%d ", a[i]);
  19.  
  20. printf("\n(swaps = %d)\n", swaps);
  21. }
  22.  
  23. int main()
  24. {
  25. int x[] = {1, 2, 3, 1, 3, 0, 6};
  26.  
  27. dupes(x, sizeof x / sizeof x[0]);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
3 1 
(swaps = 4)