├── .gitignore ├── .idea ├── .gitignore ├── compiler.xml ├── gradle.xml └── misc.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── vivek │ │ └── sortingvisualizer │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── vivek │ │ │ └── sortingvisualizer │ │ │ ├── MainActivity.kt │ │ │ ├── model │ │ │ └── Algorithm.kt │ │ │ └── ui │ │ │ ├── algorithms │ │ │ └── SortingAlogrithms.kt │ │ │ ├── components │ │ │ ├── BottomButtons.kt │ │ │ └── ScrollableChipRow.kt │ │ │ ├── screens │ │ │ ├── DrawingCanvas.kt │ │ │ └── HomeScreen.kt │ │ │ ├── theme │ │ │ ├── Color.kt │ │ │ ├── Shape.kt │ │ │ ├── Theme.kt │ │ │ └── Type.kt │ │ │ └── util │ │ │ └── DimensionsUtil.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── com │ └── vivek │ └── sortingvisualizer │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting Visualizer 2 | 3 | 4 | ## :scroll: Description 5 | Visualizing sorting algorithms 📊 for learning and fun 6 | 7 | 8 | ## :bulb: Motivation and Context 9 | I was watching some random video of sorting algorithms on YT, which seemed interesting 10 |
11 | I thought why should web developer have all the fun 😜 , so I created this 🚀 12 | 13 | ## :camera_flash: Video 14 | https://user-images.githubusercontent.com/40730402/147271639-3e0f61e8-1ddf-4aa6-b6d8-9c093cea7b1a.mp4 15 | 16 | ## :white_check_mark: Todo 17 | - [ ] Add stop functionality 18 | - [ ] Add slider for controlling elements 19 | - [ ] Add slider for controlling speed 20 | - [ ] Add more sorting algorithms 21 | 22 | ## License 23 | ``` 24 | Copyright 2021 Vivek Sharma 25 | 26 | Licensed under the Apache License, Version 2.0 (the "License"); 27 | you may not use this file except in compliance with the License. 28 | You may obtain a copy of the License at 29 | 30 | http://www.apache.org/licenses/LICENSE-2.0 31 | 32 | Unless required by applicable law or agreed to in writing, software 33 | distributed under the License is distributed on an "AS IS" BASIS, 34 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | See the License for the specific language governing permissions and 36 | limitations under the License. 37 | ``` 38 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | } 5 | 6 | android { 7 | compileSdk 31 8 | 9 | defaultConfig { 10 | applicationId "com.vivek.sortingvisualizer" 11 | minSdk 21 12 | targetSdk 31 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | vectorDrawables { 18 | useSupportLibrary true 19 | } 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | buildFeatures { 36 | compose true 37 | } 38 | composeOptions { 39 | kotlinCompilerExtensionVersion compose_version 40 | } 41 | } 42 | 43 | dependencies { 44 | implementation 'androidx.core:core-ktx:1.7.0' 45 | implementation 'androidx.appcompat:appcompat:1.4.0' 46 | implementation 'com.google.android.material:material:1.4.0' 47 | 48 | implementation "androidx.compose.ui:ui:$compose_version" 49 | implementation "androidx.compose.material:material:$compose_version" 50 | implementation "androidx.compose.material:material-icons-extended:$compose_version" 51 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" 52 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0' 53 | implementation 'androidx.activity:activity-compose:1.4.0' 54 | 55 | testImplementation 'junit:junit:4.+' 56 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 57 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 58 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" 59 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 60 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vivek/sortingvisualizer/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.vivek.sortingvisualizer", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer 2 | 3 | import android.os.Bundle 4 | import androidx.activity.ComponentActivity 5 | import androidx.activity.compose.setContent 6 | import androidx.compose.material.MaterialTheme 7 | import androidx.compose.material.Surface 8 | import com.vivek.sortingvisualizer.ui.screens.HomeScreen 9 | import com.vivek.sortingvisualizer.ui.theme.SortingVisualizerTheme 10 | 11 | class MainActivity : ComponentActivity() { 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | 15 | setContent { 16 | SortingVisualizerTheme { 17 | Surface(color = MaterialTheme.colors.background) { 18 | HomeScreen() 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/model/Algorithm.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.model 2 | 3 | enum class Algorithm(val value: String) { 4 | BUBBLE_SORT("Bubble Sort"), 5 | SELECTION_SORT("Selection Sort"), 6 | INSERTION_SORT("Insertion Sort"), 7 | QUICK_SORT("Quick Sort"), 8 | TWO_POINTER_QUICK_SORT("Two Pointer Quick Sort"), 9 | MERGE_SORT("Merge Sort") 10 | } 11 | 12 | fun getAllAlgorithms(): List { 13 | return Algorithm.values().toList() 14 | } 15 | 16 | fun getAlgorithm(value: String): Algorithm? { 17 | val map = Algorithm.values().associateBy(Algorithm::value) 18 | return map[value] 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/algorithms/SortingAlogrithms.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.algorithms 2 | 3 | import kotlinx.coroutines.delay 4 | import kotlin.random.Random 5 | 6 | fun randomize(width: Int, height: Int): MutableList { 7 | val list = mutableListOf() 8 | for (i in 0..width) { 9 | list.add(Random.nextInt(height)) 10 | } 11 | return list 12 | } 13 | 14 | suspend fun bubbleSort( 15 | list: MutableList, 16 | onUpdateItems: (MutableList) -> Unit 17 | ) { 18 | val n = list.size 19 | 20 | for (i in 0 until n - 1) { 21 | var swapped = false 22 | for (j in 0 until (n - i - 1)) { 23 | if (list[j] > list[j + 1]) { 24 | // swap 25 | val t = list[j] 26 | list[j] = list[j + 1] 27 | list[j + 1] = t 28 | swapped = true 29 | } 30 | } 31 | 32 | delay(10) 33 | onUpdateItems(list) 34 | if (!swapped) break 35 | } 36 | } 37 | 38 | suspend fun selectionSort( 39 | list: MutableList, 40 | onUpdateItems: (MutableList) -> Unit 41 | ) { 42 | val n = list.size 43 | 44 | for (i in 0 until n - 1) { 45 | var indexMin = i 46 | 47 | for (j in i + 1 until n) { 48 | if (list[j] < list[indexMin]) { 49 | indexMin = j 50 | } 51 | } 52 | // swap 53 | val temp = list[i] 54 | list[i] = list[indexMin] 55 | list[indexMin] = temp 56 | 57 | delay(10) 58 | onUpdateItems(list) 59 | } 60 | } 61 | 62 | suspend fun insertionSort( 63 | list: MutableList, 64 | onUpdateItems: (MutableList) -> Unit 65 | ) { 66 | val n = list.size 67 | 68 | for (i in 1 until n) { 69 | val value = list[i] 70 | var hole = i 71 | 72 | while (hole > 0 && list[hole - 1] > value) { 73 | list[hole] = list[hole - 1] 74 | --hole 75 | } 76 | delay(10) 77 | list[hole] = value 78 | onUpdateItems(list) 79 | } 80 | } 81 | 82 | suspend fun quickSort( 83 | list: MutableList, 84 | start: Int, 85 | end: Int, 86 | onUpdateItems: (MutableList) -> Unit 87 | ) { 88 | if (start >= end) { 89 | return 90 | } 91 | 92 | val pivotIndex = partition(list, start, end, onUpdateItems) 93 | quickSort(list, start, pivotIndex - 1, onUpdateItems) 94 | quickSort(list, pivotIndex + 1, end, onUpdateItems) 95 | 96 | delay(10) 97 | onUpdateItems(list) 98 | } 99 | 100 | suspend fun partition( 101 | list: MutableList, 102 | start: Int, 103 | end: Int, 104 | onUpdateItems: (MutableList) -> Unit 105 | ): Int { 106 | var pivotIndex = start 107 | val pivotValue = list[end] 108 | 109 | for (i in start until end) { 110 | if (list[i] < pivotValue) { 111 | // swap list[i] and list[pivotIndex] 112 | val temp = list[i] 113 | list[i] = list[pivotIndex] 114 | list[pivotIndex] = temp 115 | 116 | ++pivotIndex 117 | } 118 | 119 | delay(1) 120 | onUpdateItems(list) 121 | } 122 | 123 | // swap list[pivotIndex] and list[end] 124 | val temp = list[pivotIndex] 125 | list[pivotIndex] = list[end] 126 | list[end] = temp 127 | 128 | onUpdateItems(list) 129 | 130 | return pivotIndex 131 | } 132 | 133 | suspend fun twoPointerQuickSort( 134 | list: MutableList, 135 | left: Int, 136 | right: Int, 137 | onUpdateItems: (MutableList) -> Unit 138 | ) { 139 | if (left >= right) { 140 | return 141 | } 142 | 143 | val pivotValue = list[left] 144 | val pivotIndex = twoPQSPartition(list, left, right, pivotValue, onUpdateItems) 145 | twoPointerQuickSort(list, left, pivotIndex - 1, onUpdateItems) 146 | twoPointerQuickSort(list, pivotIndex + 1, right, onUpdateItems) 147 | } 148 | 149 | suspend fun twoPQSPartition( 150 | list: MutableList, 151 | left: Int, 152 | right: Int, 153 | pivotValue: Int, 154 | onUpdateItems: (MutableList) -> Unit 155 | ): Int { 156 | var leftI = left 157 | var rightI = right 158 | 159 | while (leftI < rightI) { 160 | while (list[leftI] <= pivotValue && leftI < rightI) { 161 | ++leftI 162 | } 163 | 164 | while (list[rightI] > pivotValue) { 165 | --rightI 166 | } 167 | 168 | if (leftI < rightI) { 169 | // swap list[leftI] and list[rightI] 170 | val temp = list[leftI] 171 | list[leftI] = list[rightI] 172 | list[rightI] = temp 173 | } 174 | 175 | delay(2) 176 | onUpdateItems(list) 177 | } 178 | 179 | // swap list[left] and list[rightI] 180 | val temp = list[left] 181 | list[left] = list[rightI] 182 | list[rightI] = temp 183 | 184 | onUpdateItems(list) 185 | 186 | return rightI 187 | } 188 | 189 | 190 | suspend fun mergeSort( 191 | list: MutableList, 192 | temp: MutableList, 193 | leftStart: Int, 194 | rightEnd: Int, 195 | onUpdateItems: (MutableList) -> Unit 196 | ) { 197 | if (leftStart >= rightEnd) { 198 | return 199 | } 200 | 201 | val mid = (leftStart + rightEnd) / 2 202 | 203 | mergeSort(list, temp, leftStart, mid, onUpdateItems) 204 | mergeSort(list, temp, mid + 1, rightEnd, onUpdateItems) 205 | merge(list, temp, leftStart, rightEnd, onUpdateItems) 206 | } 207 | 208 | suspend fun merge( 209 | list: MutableList, 210 | temp: MutableList, 211 | leftStart: Int, 212 | rightEnd: Int, 213 | onUpdateItems: (MutableList) -> Unit 214 | ) { 215 | val leftEnd = (leftStart + rightEnd) / 2 216 | val rightStart = leftEnd + 1 217 | val size = rightEnd - leftStart + 1 218 | 219 | var left = leftStart 220 | var right = rightStart 221 | var index = leftStart 222 | 223 | while (left <= leftEnd && right <= rightEnd) { 224 | if (list[left] <= list[right]) { 225 | temp[index] = list[left] 226 | left++ 227 | } else { 228 | temp[index] = list[right] 229 | right++ 230 | } 231 | index++ 232 | } 233 | 234 | while (left <= leftEnd) { 235 | temp[index] = list[left] 236 | left++ 237 | index++ 238 | } 239 | 240 | while (right <= rightEnd) { 241 | temp[index] = list[right] 242 | right++ 243 | index++ 244 | } 245 | 246 | for (i in 0 until size) { 247 | list[leftStart + i] = temp[leftStart + i] 248 | } 249 | 250 | delay(10) 251 | onUpdateItems(list) 252 | } 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/components/BottomButtons.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.components 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.clickable 5 | import androidx.compose.foundation.layout.Arrangement 6 | import androidx.compose.foundation.layout.Box 7 | import androidx.compose.foundation.layout.Row 8 | import androidx.compose.foundation.layout.fillMaxHeight 9 | import androidx.compose.foundation.layout.fillMaxWidth 10 | import androidx.compose.foundation.layout.height 11 | import androidx.compose.foundation.layout.padding 12 | import androidx.compose.foundation.layout.size 13 | import androidx.compose.foundation.shape.RoundedCornerShape 14 | import androidx.compose.material.Icon 15 | import androidx.compose.material.Text 16 | import androidx.compose.material.icons.Icons 17 | import androidx.compose.material.icons.filled.Refresh 18 | import androidx.compose.runtime.Composable 19 | import androidx.compose.ui.Alignment 20 | import androidx.compose.ui.Modifier 21 | import androidx.compose.ui.draw.clip 22 | import androidx.compose.ui.graphics.Color 23 | import androidx.compose.ui.text.font.FontWeight 24 | import androidx.compose.ui.unit.dp 25 | import androidx.compose.ui.unit.sp 26 | import com.vivek.sortingvisualizer.ui.theme.Blue 27 | import com.vivek.sortingvisualizer.ui.theme.GreenDark 28 | import com.vivek.sortingvisualizer.ui.theme.Red 29 | 30 | @Composable 31 | fun BottomButtons( 32 | isSortRunning: Boolean, 33 | onStartSorting: () -> Unit, 34 | onRandomize: () -> Unit 35 | ) { 36 | Row( 37 | modifier = Modifier 38 | .fillMaxWidth() 39 | .height(100.dp), 40 | horizontalArrangement = Arrangement.SpaceBetween 41 | ) { 42 | // Sort Button 43 | Box(modifier = Modifier 44 | .fillMaxWidth(0.8f) 45 | .fillMaxHeight() 46 | .padding(16.dp) 47 | .clip(RoundedCornerShape(12.dp)) 48 | .background(if (!isSortRunning) GreenDark else Red) 49 | .clickable { onStartSorting() } 50 | ) { 51 | Text( 52 | text = if (!isSortRunning) "SORT" else "STOP", 53 | fontWeight = FontWeight.Bold, 54 | fontSize = 20.sp, 55 | color = Color.White, 56 | modifier = Modifier 57 | .align(Alignment.Center) 58 | .padding(16.dp) 59 | ) 60 | } 61 | 62 | // Refresh Button 63 | Box(modifier = Modifier 64 | .fillMaxWidth() 65 | .fillMaxHeight() 66 | .padding(top = 16.dp, end = 16.dp, bottom = 16.dp) 67 | .clip(RoundedCornerShape(12.dp)) 68 | .background(if (!isSortRunning) Blue else Blue.copy(alpha = 0.5f)) 69 | .clickable { onRandomize() } 70 | ) { 71 | Icon( 72 | imageVector = Icons.Default.Refresh, 73 | contentDescription = "Reset", 74 | tint = Color.White, 75 | modifier = Modifier 76 | .size(28.dp) 77 | .align(Alignment.Center) 78 | ) 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/components/ScrollableChipRow.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.components 2 | 3 | import androidx.compose.foundation.ScrollState 4 | import androidx.compose.foundation.background 5 | import androidx.compose.foundation.clickable 6 | import androidx.compose.foundation.horizontalScroll 7 | import androidx.compose.foundation.layout.Box 8 | import androidx.compose.foundation.layout.Row 9 | import androidx.compose.foundation.layout.padding 10 | import androidx.compose.foundation.shape.RoundedCornerShape 11 | import androidx.compose.material.Text 12 | import androidx.compose.runtime.Composable 13 | import androidx.compose.runtime.LaunchedEffect 14 | import androidx.compose.ui.Modifier 15 | import androidx.compose.ui.draw.clip 16 | import androidx.compose.ui.graphics.Color 17 | import androidx.compose.ui.text.font.FontWeight 18 | import androidx.compose.ui.unit.dp 19 | import com.vivek.sortingvisualizer.model.Algorithm 20 | import com.vivek.sortingvisualizer.model.getAllAlgorithms 21 | import com.vivek.sortingvisualizer.ui.theme.GreenDark 22 | 23 | @Composable 24 | fun CustomChip( 25 | algoName: String, 26 | isSelected: Boolean, 27 | onClick: () -> Unit 28 | ) { 29 | Box( 30 | modifier = Modifier 31 | .padding(8.dp) 32 | .clickable { onClick() } 33 | .clip(RoundedCornerShape(8.dp)) 34 | .background(if (isSelected) GreenDark else GreenDark.copy(alpha = 0.5f)) 35 | ) { 36 | Text( 37 | text = algoName, 38 | color = Color.White, 39 | fontWeight = FontWeight.SemiBold, 40 | modifier = Modifier.padding(horizontal = 16.dp, vertical = 16.dp) 41 | ) 42 | } 43 | } 44 | 45 | @Composable 46 | fun CustomChipRow( 47 | scrollState: ScrollState, 48 | selectedAlgorithm: Algorithm, 49 | onSelectedAlgorithmChanged: (Algorithm) -> Unit, 50 | ) { 51 | LaunchedEffect(key1 = Unit) { 52 | onSelectedAlgorithmChanged(Algorithm.BUBBLE_SORT) 53 | } 54 | 55 | Row( 56 | modifier = Modifier 57 | .padding(start = 8.dp, top = 16.dp, bottom = 8.dp) 58 | .horizontalScroll(scrollState) 59 | ) { 60 | for (algorithm in getAllAlgorithms()) { 61 | CustomChip( 62 | algoName = algorithm.value, 63 | isSelected = selectedAlgorithm == algorithm, 64 | onClick = { onSelectedAlgorithmChanged(algorithm) }, 65 | ) 66 | } 67 | } 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/screens/DrawingCanvas.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.screens 2 | 3 | import androidx.compose.foundation.Canvas 4 | import androidx.compose.foundation.layout.fillMaxWidth 5 | import androidx.compose.runtime.Composable 6 | import androidx.compose.ui.Modifier 7 | import androidx.compose.ui.geometry.Offset 8 | import com.vivek.sortingvisualizer.ui.theme.GreenExtraLight 9 | 10 | @Composable 11 | fun DrawingCanvas( 12 | modifier: Modifier, 13 | items: MutableList 14 | ) { 15 | Canvas( 16 | modifier = modifier.fillMaxWidth() 17 | ) { 18 | val canvasWidth = size.width 19 | val canvasHeight = size.height 20 | 21 | items.forEachIndexed { i, height -> 22 | drawLine( 23 | start = Offset(x = (i).toFloat(), y = canvasHeight), 24 | end = Offset(x = (i).toFloat(), y = canvasHeight - height), 25 | color = GreenExtraLight, 26 | strokeWidth = 2f 27 | ) 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/screens/HomeScreen.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.screens 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.Box 5 | import androidx.compose.foundation.layout.Column 6 | import androidx.compose.foundation.layout.Spacer 7 | import androidx.compose.foundation.layout.fillMaxHeight 8 | import androidx.compose.foundation.layout.fillMaxWidth 9 | import androidx.compose.foundation.layout.height 10 | import androidx.compose.foundation.layout.padding 11 | import androidx.compose.foundation.rememberScrollState 12 | import androidx.compose.foundation.shape.RoundedCornerShape 13 | import androidx.compose.material.Scaffold 14 | import androidx.compose.material.Text 15 | import androidx.compose.runtime.Composable 16 | import androidx.compose.runtime.getValue 17 | import androidx.compose.runtime.mutableStateOf 18 | import androidx.compose.runtime.remember 19 | import androidx.compose.runtime.rememberCoroutineScope 20 | import androidx.compose.runtime.setValue 21 | import androidx.compose.ui.Modifier 22 | import androidx.compose.ui.draw.clip 23 | import androidx.compose.ui.graphics.Color 24 | import androidx.compose.ui.text.font.FontWeight 25 | import androidx.compose.ui.unit.dp 26 | import androidx.compose.ui.unit.sp 27 | import com.vivek.sortingvisualizer.model.Algorithm 28 | import com.vivek.sortingvisualizer.ui.algorithms.bubbleSort 29 | import com.vivek.sortingvisualizer.ui.algorithms.insertionSort 30 | import com.vivek.sortingvisualizer.ui.algorithms.mergeSort 31 | import com.vivek.sortingvisualizer.ui.algorithms.quickSort 32 | import com.vivek.sortingvisualizer.ui.algorithms.randomize 33 | import com.vivek.sortingvisualizer.ui.algorithms.selectionSort 34 | import com.vivek.sortingvisualizer.ui.algorithms.twoPointerQuickSort 35 | import com.vivek.sortingvisualizer.ui.components.BottomButtons 36 | import com.vivek.sortingvisualizer.ui.components.CustomChipRow 37 | import com.vivek.sortingvisualizer.ui.theme.GreenExtraDark 38 | import com.vivek.sortingvisualizer.ui.util.getScreenHeight 39 | import com.vivek.sortingvisualizer.ui.util.getScreenWidth 40 | import kotlinx.coroutines.CoroutineScope 41 | import kotlinx.coroutines.launch 42 | 43 | 44 | @Composable 45 | fun HomeScreen() { 46 | val width = getScreenWidth() 47 | val height = getScreenHeight() / 2 48 | 49 | var items by remember { mutableStateOf(mutableListOf()) } 50 | var selectedAlgorithm by remember { mutableStateOf(Algorithm.BUBBLE_SORT) } 51 | var isSortRunning by remember { mutableStateOf(false) } 52 | 53 | val onUpdateItems: (MutableList) -> Unit = { 54 | items = mutableListOf() 55 | items = it 56 | } 57 | 58 | val scope = rememberCoroutineScope() 59 | val scrollState = rememberScrollState() 60 | 61 | Scaffold( 62 | backgroundColor = GreenExtraDark, 63 | topBar = { 64 | Text( 65 | text = "Sorting Visualizer", 66 | fontWeight = FontWeight.Bold, 67 | fontSize = 24.sp, 68 | color = Color.White, 69 | modifier = Modifier.padding(start = 16.dp, top = 8.dp) 70 | ) 71 | }, 72 | bottomBar = { 73 | Box( 74 | modifier = Modifier 75 | .fillMaxWidth() 76 | .clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)) 77 | .background(Color.White) 78 | ) { 79 | Column { 80 | CustomChipRow( 81 | scrollState = scrollState, 82 | selectedAlgorithm = selectedAlgorithm, 83 | onSelectedAlgorithmChanged = { 84 | selectedAlgorithm = it 85 | items = randomize(width, height) 86 | } 87 | ) 88 | 89 | Spacer(modifier = Modifier.height(22.dp)) 90 | 91 | BottomButtons( 92 | isSortRunning = isSortRunning, 93 | onStartSorting = { 94 | startSortingAlgorithm( 95 | algorithm = selectedAlgorithm, 96 | scope = scope, 97 | items = items, 98 | onUpdateItems = onUpdateItems, 99 | onSortRunning = { isSortRunning = it } 100 | ) 101 | }, 102 | onRandomize = { 103 | items = randomize(width, height) 104 | } 105 | ) 106 | } 107 | } 108 | } 109 | ) { 110 | DrawingCanvas( 111 | modifier = Modifier.fillMaxHeight(0.7f), 112 | items = items 113 | ) 114 | } 115 | } 116 | 117 | fun startSortingAlgorithm( 118 | algorithm: Algorithm, 119 | scope: CoroutineScope, 120 | items: MutableList, 121 | onUpdateItems: (MutableList) -> Unit, 122 | onSortRunning: (Boolean) -> Unit 123 | ) { 124 | when (algorithm) { 125 | Algorithm.BUBBLE_SORT -> { 126 | scope.launch { 127 | onSortRunning(true) 128 | 129 | bubbleSort( 130 | list = items, 131 | onUpdateItems = onUpdateItems 132 | ) 133 | 134 | onSortRunning(false) 135 | } 136 | } 137 | 138 | Algorithm.SELECTION_SORT -> { 139 | scope.launch { 140 | onSortRunning(true) 141 | 142 | selectionSort( 143 | list = items, 144 | onUpdateItems = onUpdateItems 145 | ) 146 | 147 | onSortRunning(false) 148 | } 149 | } 150 | 151 | Algorithm.INSERTION_SORT -> { 152 | scope.launch { 153 | onSortRunning(true) 154 | 155 | insertionSort( 156 | list = items, 157 | onUpdateItems = onUpdateItems 158 | ) 159 | 160 | onSortRunning(false) 161 | } 162 | } 163 | 164 | Algorithm.QUICK_SORT -> { 165 | scope.launch { 166 | onSortRunning(true) 167 | 168 | quickSort( 169 | list = items, 170 | start = 0, 171 | end = items.size - 1, 172 | onUpdateItems = onUpdateItems 173 | ) 174 | 175 | onSortRunning(false) 176 | } 177 | } 178 | 179 | Algorithm.TWO_POINTER_QUICK_SORT -> { 180 | scope.launch { 181 | onSortRunning(true) 182 | 183 | twoPointerQuickSort( 184 | list = items, 185 | left = 0, 186 | right = items.size - 1, 187 | onUpdateItems = onUpdateItems 188 | ) 189 | 190 | onSortRunning(false) 191 | } 192 | } 193 | 194 | Algorithm.MERGE_SORT -> { 195 | scope.launch { 196 | onSortRunning(true) 197 | 198 | val list = items 199 | val temp = mutableListOf() 200 | items.forEach { temp.add(it) } 201 | 202 | mergeSort( 203 | list = list, 204 | temp = temp, 205 | leftStart = 0, 206 | rightEnd = list.size - 1, 207 | onUpdateItems = onUpdateItems 208 | ) 209 | 210 | onSortRunning(false) 211 | } 212 | } 213 | } 214 | } 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val Purple200 = Color(0xFFBB86FC) 6 | val Purple500 = Color(0xFF6200EE) 7 | val Purple700 = Color(0xFF3700B3) 8 | val Teal200 = Color(0xFF03DAC5) 9 | 10 | val GreenDark = Color(0xFF24B273) 11 | val GreenExtraDark = Color(0xFF243D44) 12 | val GreenExtraLight = Color(0xFF2DEA8F) 13 | val Blue = Color(0xFF6356E5) 14 | val Red = Color(0xFFF83E4B) -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/theme/Shape.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.theme 2 | 3 | import androidx.compose.foundation.shape.RoundedCornerShape 4 | import androidx.compose.material.Shapes 5 | import androidx.compose.ui.unit.dp 6 | 7 | val Shapes = Shapes( 8 | small = RoundedCornerShape(4.dp), 9 | medium = RoundedCornerShape(4.dp), 10 | large = RoundedCornerShape(0.dp) 11 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.theme 2 | 3 | import androidx.compose.foundation.isSystemInDarkTheme 4 | import androidx.compose.material.MaterialTheme 5 | import androidx.compose.material.darkColors 6 | import androidx.compose.material.lightColors 7 | import androidx.compose.runtime.Composable 8 | 9 | private val DarkColorPalette = darkColors( 10 | primary = Purple200, 11 | primaryVariant = Purple700, 12 | secondary = Teal200 13 | ) 14 | 15 | private val LightColorPalette = lightColors( 16 | primary = Purple500, 17 | primaryVariant = Purple700, 18 | secondary = Teal200 19 | 20 | /* Other default colors to override 21 | background = Color.White, 22 | surface = Color.White, 23 | onPrimary = Color.White, 24 | onSecondary = Color.Black, 25 | onBackground = Color.Black, 26 | onSurface = Color.Black, 27 | */ 28 | ) 29 | 30 | @Composable 31 | fun SortingVisualizerTheme( 32 | darkTheme: Boolean = isSystemInDarkTheme(), 33 | content: @Composable() () -> Unit 34 | ) { 35 | val colors = if (darkTheme) { 36 | DarkColorPalette 37 | } else { 38 | LightColorPalette 39 | } 40 | 41 | MaterialTheme( 42 | colors = colors, 43 | typography = Typography, 44 | shapes = Shapes, 45 | content = content 46 | ) 47 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.theme 2 | 3 | import androidx.compose.material.Typography 4 | import androidx.compose.ui.text.TextStyle 5 | import androidx.compose.ui.text.font.FontFamily 6 | import androidx.compose.ui.text.font.FontWeight 7 | import androidx.compose.ui.unit.sp 8 | 9 | // Set of Material typography styles to start with 10 | val Typography = Typography( 11 | body1 = TextStyle( 12 | fontFamily = FontFamily.Default, 13 | fontWeight = FontWeight.Normal, 14 | fontSize = 16.sp 15 | ) 16 | /* Other default text styles to override 17 | button = TextStyle( 18 | fontFamily = FontFamily.Default, 19 | fontWeight = FontWeight.W500, 20 | fontSize = 14.sp 21 | ), 22 | caption = TextStyle( 23 | fontFamily = FontFamily.Default, 24 | fontWeight = FontWeight.Normal, 25 | fontSize = 12.sp 26 | ) 27 | */ 28 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/vivek/sortingvisualizer/ui/util/DimensionsUtil.kt: -------------------------------------------------------------------------------- 1 | package com.vivek.sortingvisualizer.ui.util 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.ui.platform.LocalContext 5 | 6 | @Composable 7 | fun getScreenWidth(): Int { 8 | val metrics = LocalContext.current.resources.displayMetrics 9 | return metrics.widthPixels 10 | } 11 | 12 | @Composable 13 | fun getScreenHeight(): Int { 14 | val metrics = LocalContext.current.resources.displayMetrics 15 | return metrics.heightPixels 16 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/V9vek/Sorting-Visualizer/51017f3cc312febb63dcdc028a8c995e8ec9810a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | #FF243D44 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SortingVisualizer 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 22 | 23 |