├── .gitignore ├── Algorithm ├── InsertionSort │ └── InsertionSort.dart ├── BubbleSort │ └── BubbleSort.dart ├── SelectionSort │ └── SelectionSort.dart ├── GnomeSort │ └── GnomeSort.dart ├── QuickSort │ └── QuickSort.dart ├── CombSort │ └── CombSort.dart ├── CocktailSort │ └── CocktailSort.dart ├── HeapSort │ └── HeapSort.dart ├── MergeSort │ └── MergeSort.dart └── TimSort │ └── TimSort.dart ├── README.md └── DataStructure └── LinkedList └── LinkedList.dart /.gitignore: -------------------------------------------------------------------------------- 1 | generated/* 2 | -------------------------------------------------------------------------------- /Algorithm/InsertionSort/InsertionSort.dart: -------------------------------------------------------------------------------- 1 | void insertionSort(List list) { 2 | if (list == null || list.length == 0) return; 3 | int n = list.length; 4 | int temp, i, j; 5 | 6 | for(i=1; i< n; i++) { 7 | temp = list[i]; 8 | j=i-1; 9 | while(j >= 0 && temp < list[j] ) { 10 | list[j+1] = list[j]; 11 | --j; 12 | } 13 | list[j+1] = temp; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithm/BubbleSort/BubbleSort.dart: -------------------------------------------------------------------------------- 1 | void bubbleSort(List list) { 2 | if (list == null || list.length == 0) return; 3 | 4 | int n = list.length; 5 | int i, step; 6 | for (step = 0; step < n; step++) { 7 | for ( i = 0; i < n - step - 1; i++) { 8 | if (list[i] > list[i + 1]) { 9 | swap(list, i); 10 | } 11 | } 12 | } 13 | } 14 | 15 | void swap(List list, int i) { 16 | int temp = list[i]; 17 | list[i] = list[i+1]; 18 | list[i+1] = temp; 19 | } -------------------------------------------------------------------------------- /Algorithm/SelectionSort/SelectionSort.dart: -------------------------------------------------------------------------------- 1 | void selectionSort(List list) { 2 | if (list == null || list.length == 0) return; 3 | int n = list.length; 4 | int i, steps; 5 | for (steps = 0; steps < n; steps++) { 6 | for (i = steps + 1; i < n; i++) { 7 | if(list[steps] > list[i]) { 8 | swap(list, steps, i); 9 | } 10 | } 11 | } 12 | } 13 | 14 | void swap(List list, int steps, int i) { 15 | int temp = list[steps]; 16 | list[steps] = list[i]; 17 | list[i] = temp; 18 | } -------------------------------------------------------------------------------- /Algorithm/GnomeSort/GnomeSort.dart: -------------------------------------------------------------------------------- 1 | void gnomeSort(List list) { 2 | int length = list.length; 3 | if (list == null || length == 0) return; 4 | int first = 1; 5 | int second = 2; 6 | 7 | 8 | while (first < length) { 9 | if (list[first - 1] <= list[first]) { 10 | first = second; 11 | second++; 12 | } else { 13 | int temp = list[first - 1]; 14 | list[first - 1] = list[first]; 15 | list[first] = temp; 16 | first -= 1; 17 | if (first ==0){ 18 | first = 1; 19 | second = 2; 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Algorithm/QuickSort/QuickSort.dart: -------------------------------------------------------------------------------- 1 | int partition(List list, int low, int high) { 2 | if (list == null || list.length == 0) return 0; 3 | int pivot = list[high]; 4 | int i = low - 1; 5 | 6 | for (int j = low; j < high; j++) { 7 | if (list[j] <= pivot) { 8 | i++; 9 | swap(list, i, j); 10 | } 11 | swap(list, i+1, high); 12 | return i+1; 13 | } 14 | 15 | void swap(List list, int i, int j) { 16 | int temp = list[i]; 17 | list[i] = list[j]; 18 | list[j] = temp; 19 | } 20 | 21 | void quickSort(List list, int low, int high) { 22 | if (low < high) { 23 | int pi = partition(list, low, high); 24 | quickSort(list, low, pi-1); 25 | quickSort(list, pi+1, high); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Algorithm/CombSort/CombSort.dart: -------------------------------------------------------------------------------- 1 | void combSort(List list) { 2 | int gapValue = list.length; 3 | double shrink = 1.3; 4 | bool sorted = false; 5 | 6 | while(!sorted) { 7 | gapValue = (gapValue/shrink).floor(); 8 | if (gapValue > 1) { 9 | sorted = false; 10 | } else { 11 | gapValue = 1; 12 | sorted = true; 13 | } 14 | 15 | int i = 0; 16 | while (i + gapValue < list.length) { 17 | if (list[i] > list[i + gapValue]) { 18 | swap(list, i, gapValue); 19 | sorted = false; 20 | } 21 | i++; 22 | } 23 | } 24 | } 25 | 26 | void swap(List list, int i, int gapValue) { 27 | int temp = list[i]; 28 | list[i] = list[i+gapValue]; 29 | list[i+gapValue] = temp; 30 | } -------------------------------------------------------------------------------- /Algorithm/CocktailSort/CocktailSort.dart: -------------------------------------------------------------------------------- 1 | void cocktailSort(List list) { 2 | bool swapped = true; 3 | do { 4 | swapped = false; 5 | for (int i = 0; i < list.length - 2; i++) { 6 | swapped = swapItemCocktail(list, i, swapped); 7 | } 8 | 9 | if (swapped) { 10 | swapped = false; 11 | for (int i = list.length - 2; i >=0 ; i--) { 12 | swapped = swapItemCocktail(list, i, swapped); 13 | } 14 | } 15 | } while (swapped); 16 | } 17 | 18 | bool swapItemCocktail(List list, int i, bool swapped) { 19 | if(list[i] > list[i+1]) { 20 | swap(list, i); 21 | swapped = true; 22 | } 23 | return swapped; 24 | } 25 | 26 | void swap(List list, int i) { 27 | int temp = list[i]; 28 | list[i] = list[i+1]; 29 | list[i+1] = temp; 30 | } -------------------------------------------------------------------------------- /Algorithm/HeapSort/HeapSort.dart: -------------------------------------------------------------------------------- 1 | void heapify(List list, int n, int i) { 2 | int largest = i; 3 | int l = 2*i + 1; 4 | int r = 2*i + 2; 5 | 6 | if (l < n && list[l] > list[largest]) { 7 | largest = l; 8 | } 9 | 10 | if (r < n && list[r] > list[largest]) { 11 | largest = r; 12 | } 13 | 14 | if (largest != i) { 15 | swapList(list, i, largest); 16 | heapify(list, n, largest); 17 | } 18 | } 19 | 20 | void swapList(List list, int i, int largest) { 21 | int swap = list[i]; 22 | list[i] = list[largest]; 23 | list[largest] = swap; 24 | } 25 | 26 | void heapSort(List list){ 27 | int n = list.length; 28 | for (int i = (n ~/ 2 ) ; i >= 0; i--){ 29 | heapify(list, n, i); 30 | } 31 | 32 | for (int i = n-1; i>=0; i--) { 33 | swap(list, i); 34 | heapify(list, i, 0); 35 | } 36 | } 37 | 38 | void swap(List list, int i) { 39 | int temp = list[0]; 40 | list[0] = list[i]; 41 | list[i] = temp; 42 | } -------------------------------------------------------------------------------- /Algorithm/MergeSort/MergeSort.dart: -------------------------------------------------------------------------------- 1 | void merge(List list, int leftIndex, int middleIndex, int rightIndex) { 2 | int leftSize = middleIndex - leftIndex + 1; 3 | int rightSize = rightIndex - middleIndex; 4 | 5 | List leftList = new List(leftSize); 6 | List rightList = new List(rightSize); 7 | 8 | for (int i = 0; i < leftSize; i++) leftList[i] = list[leftIndex + i]; 9 | for (int j = 0; j < rightSize; j++) rightList[j] = list[middleIndex + j + 1]; 10 | 11 | int i = 0, j = 0; 12 | int k = leftIndex; 13 | 14 | while (i < leftSize && j < rightSize) { 15 | if (leftList[i] <= rightList[j]) { 16 | list[k] = leftList[i]; 17 | i++; 18 | } else { 19 | list[k] = rightList[j]; 20 | j++; 21 | } 22 | k++; 23 | } 24 | 25 | while (i < leftSize) { 26 | list[k] = leftList[i]; 27 | i++; 28 | k++; 29 | } 30 | 31 | while (j < rightSize) { 32 | list[k] = rightList[j]; 33 | j++; 34 | k++; 35 | } 36 | } 37 | 38 | void mergeSort(List list, int leftIndex, int rightIndex) { 39 | if (leftIndex < rightIndex) { 40 | int middleIndex = (rightIndex + leftIndex) ~/ 2; 41 | 42 | mergeSort(list, leftIndex, middleIndex); 43 | mergeSort(list, middleIndex + 1, rightIndex); 44 | 45 | merge(list, leftIndex, middleIndex, rightIndex); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dart Data Structure & Algorithm 2 | 3 | Clean and simple in Dart. There are several data structure and algorithm are implemented in Dart language. 4 | 5 | ##### Sorting algorithms 6 | - [Quick Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/QuickSort/QuickSort.dart) 7 | - [Merge Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/MergeSort/MergeSort.dart) 8 | - [Tim Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/TimSort/TimSort.dart) 9 | - [Heap Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/HeapSort/HeapSort.dart) 10 | - [Bubble Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/BubbleSort/BubbleSort.dart) 11 | - [Insertion Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/BubbleSort/BubbleSort.dart) 12 | - [Selection Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/SelectionSort/SelectionSort.dart) 13 | - [Comb Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/CombSort/CombSort.dart) 14 | - [Cocktail Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/CocktailSort/CocktailSort.dart) 15 | - [Gnome Sort](https://github.com/liemvo/Dart_DataStructure_Algorithm/blob/master/Algorithm/GnomeSort/GnomeSort.dart) 16 | 17 | ## License 18 | 19 | This project is licensed under the **MIT License**. 20 | 21 | ## Author 22 | ** Liem Vo ** - [Profile](https://github.com/liemvo) 23 | -------------------------------------------------------------------------------- /Algorithm/TimSort/TimSort.dart: -------------------------------------------------------------------------------- 1 | import 'dart:math'; 2 | const int RUN = 32; 3 | void insertionSort(List list, int left, int right) 4 | { 5 | for (int i = left + 1; i <= right; i++) { 6 | int temp = list[i]; 7 | int j = i - 1; 8 | while (j >= left && list[j] > temp) { 9 | list[j+1] = list[j]; 10 | j--; 11 | } 12 | list[j+1] = temp; 13 | } 14 | } 15 | 16 | void merge(List list, int left, int middle, int right) { 17 | int length1 = middle - left + 1, length2 = right - middle; 18 | List leftList = new List(length1), rightList = new List(length2); 19 | 20 | for(int i = 0; i < length1; i++) { 21 | leftList[i] = list[left + i]; 22 | } 23 | 24 | for(int i = 0; i { 2 | Node head; 3 | 4 | void insertFirst(T t){ 5 | Node node = new Node(t); 6 | if (head != null){ 7 | node.next = head; 8 | } 9 | head = node; 10 | } 11 | 12 | void insertLast(T t) { 13 | Node node = new Node(t); 14 | if (head == null){ 15 | head = node; 16 | return; 17 | } 18 | Node current = head; 19 | while(current.next != null){ 20 | current = current.next; 21 | } 22 | current.next = node; 23 | } 24 | 25 | bool removeByValue(T t){ 26 | if (head == null) { 27 | return false; 28 | } 29 | 30 | if (head.value == t) { 31 | head = head.next; 32 | return true; 33 | } 34 | 35 | Node current = head; 36 | bool isExit = false; 37 | while(current.next != null || !isExit){ 38 | current = current.next; 39 | if (current.value == T){ 40 | isExit == true; 41 | } 42 | } 43 | 44 | return isExit; 45 | } 46 | 47 | bool removeByIndex(int i){ 48 | if (head == null) return false; 49 | if (i < 0) return false; 50 | if (i == 0) { 51 | head = head.next; 52 | return true; 53 | } 54 | 55 | Node current = head; 56 | for (int j = 1; j < i; j++){ 57 | current = current.next; 58 | if(current.next == null){ 59 | return false; 60 | } 61 | } 62 | 63 | current.next = current.next.next; 64 | return true; 65 | } 66 | 67 | } 68 | class Node { 69 | Node next; 70 | T value; 71 | 72 | 73 | Node(T value){ 74 | this.value = value; 75 | next = null; 76 | } 77 | /*Node(int value, Node next) { 78 | this.next = next; 79 | this.value = value; 80 | } 81 | 82 | bool add(Node node) { 83 | Node tmpNode = this; 84 | while(tmpNode.next != null) { 85 | tmpNode = tmpNode.next; 86 | } 87 | tmpNode.next = node; 88 | return true; 89 | } 90 | 91 | Node reverse(Node node) { 92 | Node nextNode = null; 93 | Node current = node; 94 | Node previous = null; 95 | 96 | while(current != null) { 97 | nextNode = current.next; 98 | current.next = previous; 99 | previous = current; 100 | current = nextNode; 101 | } 102 | node = previous; 103 | return node; 104 | }*/ 105 | } 106 | --------------------------------------------------------------------------------