Hi, I'm pretty new to java and I'm trying to work on this sorting algorithm question.
here's the main bit:ummm it looks long and tedious but it's basically just the merge sort algorithm I've been given.Code:public static void chooseSongs(ArrayList<Song> l, int k) { int []tmpArray = new int[a.length]; mergeSort(a,tmpArray,0,a.length - 1); } private static void mergeSort(int[]a, int[]tmpArray, int left, int right) { if( left < right) { int centre = (left + right) /2; mergeSort(a,tmpArray, left, centre); mergeSort(a.tmpArray, centre + 1, right); merge(a, tmpArray, left, centre + 1, right); } } private static void merge(int[]a, int[]tmpArray, int leftPos, int rightPos, int rightEnd){ int leftEnd = rightPos - 1; int tmpPos = leftPos; int leftBeg = leftPos; while(leftPos <= leftEnd && rightPos <= rightEnd){ if(a[leftPos] < a[rightPos]){ tmpArray[tmpPos++] = a[leftPos++]; }else{ tmpArray[tmpPos++] = a[rightPos++]; } } while(leftPos <= leftEnd){ tmpArray[tmpPos++] = a[leftPos++]; } while(rightPos <= rightEnd){ tmpArray[tmpPos++] =a[rightPos++]; } for(tmpPos = leftBeg; tmpPos <= rightEnd; tmpPos++){ a[tmpPos] = tmpArray{tmpPos}; } }
The code is using arraylist but to make it to work I'll have to change it toarrays. Because I'm a newbie and my java skill is poor I don't really know what to do. Any help would be great.
ps: I've attached the code for the whole thing if the above looks confusing (this application is suppose to sort a list of songname, artist and time into order)


Reply With Quote


Bookmarks