Skip to content

Added linear search. #1

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/src/main/java/com/waleska404/algorithms/di/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.waleska404.algorithms.domain.bubblesort.BubbleSort
import com.waleska404.algorithms.domain.bubblesort.BubbleSortImpl
import com.waleska404.algorithms.domain.dijkstra.Dijkstra
import com.waleska404.algorithms.domain.dijkstra.DijkstraImpl
import com.waleska404.algorithms.domain.linearsearch.LinearSearch
import com.waleska404.algorithms.domain.linearsearch.LinearSearchImpl
import com.waleska404.algorithms.domain.quicksort.QuickSort
import com.waleska404.algorithms.domain.quicksort.QuickSortImpl
import dagger.Module
Expand All @@ -28,4 +30,8 @@ class DataModule {
@Provides
fun providesDijkstra(): Dijkstra = DijkstraImpl()

@Singleton
@Provides
fun providesLinearSearch(): LinearSearch = LinearSearchImpl()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.waleska404.algorithms.domain.linearsearch
import kotlinx.coroutines.flow.Flow
interface LinearSearch {
suspend fun compare(list: MutableList<Int>, searchItem: Int, position: Int): Flow<LinearSearchDomainModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.waleska404.algorithms.domain.linearsearch

data class LinearSearchDomainModel(val position: Int, val elementFound: Boolean)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.waleska404.algorithms.domain.linearsearch

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class LinearSearchImpl @Inject constructor() : LinearSearch {
override suspend fun compare(
list: MutableList<Int>,
searchItem: Int,
position: Int
): Flow<LinearSearchDomainModel> {
return flow {
emit(LinearSearchDomainModel(position, list[position] == searchItem))
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/waleska404/algorithms/ui/core/Navigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.navigation.compose.composable
import com.waleska404.algorithms.ui.bubblesort.BubbleSortScreen
import com.waleska404.algorithms.ui.dijkstra.DijkstraScreen
import com.waleska404.algorithms.ui.home.HomeScreen
import com.waleska404.algorithms.ui.linearsearch.LinearSearchScreen
import com.waleska404.algorithms.ui.quicksort.QuickSortScreen

@RequiresApi(Build.VERSION_CODES.O)
Expand All @@ -34,6 +35,11 @@ fun ContentWrapper(navigationController: NavHostController) {
navigationController.navigate(
Routes.Dijkstra.route
)
},
navigateToLinearSearch = {
navigationController.navigate(
Routes.LinearSearch.route
)
}
)

Expand All @@ -59,6 +65,13 @@ fun ContentWrapper(navigationController: NavHostController) {
}
)
}
composable(route = Routes.LinearSearch.route) {
LinearSearchScreen(
navigateToHome = {
navigationController.popBackStack()
}
)
}
}
}

Expand All @@ -67,4 +80,5 @@ sealed class Routes(val route: String) {
object BubbleSort : Routes("bubble_sort")
object QuickSort : Routes("quick_sort")
object Dijkstra : Routes("dijkstras_algorithm")
object LinearSearch : Routes("linear_search")
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import com.waleska404.algorithms.ui.core.components.CustomCard
fun HomeScreen(
navigateToBubbleSort: () -> Unit,
navigateToQuickSort: () -> Unit,
navigateToDijkstra: () -> Unit
navigateToDijkstra: () -> Unit,
navigateToLinearSearch: () -> Unit,
) {
Column(
modifier = Modifier
Expand Down Expand Up @@ -69,6 +70,14 @@ fun HomeScreen(
iconDescription = R.string.sort_descending_icon,
navigateToAlgorithm = navigateToDijkstra
)

// linear search algorithm
AlgorithmListItem(
title = R.string.linear_search,
icon = R.drawable.route,
iconDescription = R.string.sort_descending_icon,
navigateToAlgorithm = navigateToLinearSearch
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.waleska404.algorithms.ui.linearsearch

data class LinearSearchList(
val list: List<LinearSearchItem>
) {
fun toDataList(): MutableList<Int> {
return list.map {
it.value
}.toMutableList()
}
}

data class LinearSearchItem(
val value: Int,
var position: Int,
var itemFound: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package com.waleska404.algorithms.ui.linearsearch

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.waleska404.algorithms.R
import com.waleska404.algorithms.ui.core.components.CustomIconButton
import com.waleska404.algorithms.ui.core.components.CustomSlider
import com.waleska404.algorithms.ui.core.components.CustomTopAppBar
import java.util.UUID
import kotlin.random.Random

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun LinearSearchScreen(
linearSearchViewModel: LinearSearchViewModel = hiltViewModel(),
navigateToHome: () -> Boolean,
) {
val list: LinearSearchList by linearSearchViewModel.list.collectAsState()
var searchItemVal by remember {
mutableIntStateOf(Random.nextInt(1, 99))
}
val currentIndex = linearSearchViewModel.currentIndex.collectAsState()
val itemFound = linearSearchViewModel.searchItemFound.collectAsState()
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.primary),
) {
CustomTopAppBar(navigateToHome = navigateToHome, title = R.string.linear_search)
Spacer(modifier = Modifier.height(10.dp))

LazyRow(
modifier = Modifier
.padding(horizontal = 15.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.Bottom
) {
items(list.list, key = {
UUID.randomUUID().toString()
}) {
LinearSearchItem(item = it)
}
}

Spacer(modifier = Modifier.height(10.dp))

Text(
text = "i = ${currentIndex.value}, current element = ${if (currentIndex.value == -1) -1 else list.list[currentIndex.value].value}, search key = $searchItemVal\n ${if (currentIndex.value > list.list.size - 1) "Element not found, press reset to start again." else if (!itemFound.value) "No match and continue to search for the next match." else "Item found at index ${currentIndex.value}"}",
fontWeight = FontWeight.Medium,
fontSize = 18.sp,
color = MaterialTheme.colorScheme.secondary,
textAlign = TextAlign.Center,
modifier = Modifier.padding(horizontal = 15.dp)
)

Spacer(modifier = Modifier.height(10.dp))

BottomButtons(
stepOver = { linearSearchViewModel.stepOver(searchItemVal) },
sliderChange = linearSearchViewModel::randomizeCurrentList,
searchItemChange = {
searchItemVal = it
},
resetClick = linearSearchViewModel::randomizeCurrentList,
listSizeInit = 10,
isEnabled = !itemFound.value && currentIndex.value < list.list.size - 1,
searchItem = searchItemVal
)
}
}

@Composable
fun LinearSearchItem(item: LinearSearchItem) {
Column(modifier = Modifier.padding(horizontal = 5.dp)) {
Box(
modifier = Modifier.size(50.dp).clip(
RoundedCornerShape(15.dp)
).background(MaterialTheme.colorScheme.onSecondary),
contentAlignment = Alignment.Center,
) {
Text(
text = item.value.toString(), fontWeight = FontWeight.Bold,
fontSize = 22.sp,
color = MaterialTheme.colorScheme.secondary,
textAlign = TextAlign.Center
)
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.O)
@Composable
private fun BottomButtons(
stepOver: () -> Unit,
sliderChange: (Int) -> Unit,
searchItemChange: (Int) -> Unit,
resetClick: () -> Unit,
modifier: Modifier = Modifier,
listSizeInit: Int,
isEnabled: Boolean,
searchItem: Int
) {
var sliderValue by remember { mutableFloatStateOf(listSizeInit.toFloat()) }
Column(
modifier = modifier.padding(15.dp)
) {
// range slider
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(id = R.string.list_size),
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.secondary
)
CustomSlider(
modifier = Modifier.weight(1f),
value = sliderValue,
valueRange = 1f..10f,
enabled = isEnabled,
onValueChange = {
val newValue = it.toInt()
if (newValue != sliderValue.toInt()) {
sliderValue = newValue.toFloat()
sliderChange(sliderValue.toInt())
}
},
color = MaterialTheme.colorScheme.secondary,
disabledColor = MaterialTheme.colorScheme.surface,
textThumbColor = MaterialTheme.colorScheme.primary
)
}
Row(verticalAlignment = Alignment.CenterVertically) {

Text(
text = stringResource(id = R.string.search_key),
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.secondary
)

OutlinedTextField(
value = searchItem.toString(),
onValueChange = {
if (it.length <= 2) searchItemChange.invoke(if (it.isNotEmpty()) it.toInt() else 0)
},
enabled = isEnabled,
modifier = Modifier
.size(50.dp),
textStyle = TextStyle(
color = MaterialTheme.colorScheme.secondary,
fontSize = 14.sp,
fontWeight = FontWeight.W500
),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done,
keyboardType = KeyboardType.Number
),
colors = TextFieldDefaults.outlinedTextFieldColors(
containerColor = MaterialTheme.colorScheme.onSecondary,
focusedBorderColor = MaterialTheme.colorScheme.onSecondary,
unfocusedBorderColor = MaterialTheme.colorScheme.onSecondary
),
)
}

Spacer(modifier = Modifier.height(10.dp))

Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
CustomIconButton(
modifier = Modifier
.weight(1f),
text = stringResource(id = R.string.step),
onClick = { stepOver() },
iconResource = R.drawable.baseline_keyboard_arrow_right_24,
iconDescriptionResource = R.string.step,
enabled = isEnabled
)

CustomIconButton(
modifier = Modifier
.weight(1f),
text = stringResource(id = R.string.reset),
onClick = resetClick,
iconResource = R.drawable.shines,
iconDescriptionResource = R.string.reset,
)
}
}
}
Loading