Sort

Run Settings
LanguageDart
Language Version
Run Command
void main() { // List letters = ['a','b','r','g','j']; // List basket = [1,3,4,67,78,4,3,4,9]; // basket.sort(); // print(basket); dynamic numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]; bubbleSort(array) { for(var i = 0; i < array.length-1; i++){ for(var j = 1+i; j < array.length; j++){ if(array[i] > array[j]){ int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } //Code here } selectionSort(array) { int smallestItemindex = 0; for(var i = 0; i < array.length-1; i++){ // Set current index as minimum int min = i; int temp = array[i]; for(var j = 1+i; j < array.length; j++){ if(array[j] < array[min]){ min = j; } } //frist item updated with the lowest in the list array[i] = array[min]; //then change the min to the new min with the previous first in the list array[min] = temp; } //Code here } insertionSort(array) { for(var i = 0; i < array.length; i++){ // if current element is less than the first element then remove from the index and insert at the first index if(array[i] < array[0]){ array.insert(0, array.removeAt(i)); }else{ // else loop through the current sorted array and check where current index is greater //than the previous element and less than the next element 3 56 current index = 4 so here 4 is //greater than 3 and less than 5 so it will enter inbetween them for(var j = 1; j< i; j++){ // if(array[i] > array[j-1] && array[i]< array[j]){ array.insert(j, array.removeAt(i)); } } } } } mergeSort (array) { if (array.length == 1) { return array } // Split Array in into right and left // return for each recursion the sorted and merge // see the left and right as an array const length = array.length; int mid = numbers.length ~/ 2; // Find middle index List<int> leftArray = numbers.sublist(0, mid); // First half List<int> rightArray = numbers.sublist(mid); // Second half return merge( mergeSort(left), mergeSort(right) ) } merge(left, right){ // compare the left and right array and insert where suppose // just take it as the left and right is not sorted // you can think it as [1 2] and [4 3] as your first input i = 0; j= 0 List arr =[]; while(i > left.length && j > right.length ){ //compare the first item in the left and right array if(left[i] < right[j] ){ // swap if the left is greater than the right arr.add(left[i]); // int temp = left[i]; // left[i] = right[j]; // right[j] = temp; i++; }else{ arr.add(right[j]); j++; } } return } const answer = mergeSort(numbers); print(answer); insertionSort(numbers); print(numbers); }
Editor Settings
Theme
Key bindings
Full width
Lines