Mourgy

sort

Sep 28th, 2020 (edited)
1,261
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class sort(){
  2.  
  3.   public static LinkedList<Movies> sortMovies(LinkedList<Movies> list){
  4.     Movies movieList[] = list.toArray(new Movies[list.size()]);
  5.     int listSize = list.size();
  6.     recursiveSort(movieList, listSize);
  7.     LinkedList<Movies> sortedList = arrayToLinkedList(movieList);
  8.     return sortedList;
  9.   }
  10.  
  11.   static void recursiveSort(Movies arr[], int n)
  12.     {
  13.         // Base case
  14.         if (n <= 1)
  15.             return;
  16.        
  17.         // Sort first n-1 elements
  18.         recursiveSort( arr, n-1 );
  19.        
  20.         // Insert last element at its correct position
  21.         // in sorted array.
  22.         Movies last = arr[n-1];
  23.         int j = n-2;
  24.        
  25.         /* Move elements of arr[0..i-1], that are
  26.           greater than key, to one position ahead
  27.           of their current position */
  28.         while (j >= 0 && arr[j] > last)
  29.         {
  30.             arr[j+1] = arr[j];
  31.             j--;
  32.         }
  33.         arr[j+1] = last;
  34.     }
  35.  
  36.   LinkedList<Movies> arrayToLinkedList(Movies arr[]){
  37.     LinkedList<Movies> newList = new LinkedList<Movies>();
  38.     for(Movies mov : arr){
  39.       newList.add(mov);
  40.     }
  41.     return newList;
  42.   }
  43.  
  44.  
  45. }
RAW Paste Data