Advertisement
Guest User

linear_sort

a guest
Jan 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. %% Implement your solution for the sorting function in here: %%
  2. function [sorted_arr] = linear_sort(arr)
  3.  
  4. positive_arr = arr(arr > 0);
  5. negative_arr = -arr(arr < 0);
  6. positive_hist_arr = zeros(1,max(positive_arr));
  7. positive_hist_arr(positive_arr) = positive_arr;
  8. negative_hist_arr = zeros(1, max(negative_arr));
  9. negative_hist_arr(negative_arr) = negative_arr;
  10. correct_negative_arr = find(negative_hist_arr > 0);
  11. correct_negative_arr = -correct_negative_arr(end:-1:1);
  12.  
  13. if (sum(arr == 0))
  14. sorted_arr = [correct_negative_arr, 0, find(positive_hist_arr >0)];
  15. else
  16. sorted_arr = [correct_negative_arr, find(positive_hist_arr >0)];
  17. end
  18.  
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement