Skip to content

Instantly share code, notes, and snippets.

View viraj19r's full-sized avatar
💭
Learning

Viraj Sharma viraj19r

💭
Learning
View GitHub Profile
class Medicine {
var name,brand,typeOf;
int expiry;
Medicine(
var medicineName, var medicineBrand, int expiryYear, var typeOfMedicine) {
name = medicineName;
brand = medicineBrand;
expiry = expiryYear;
typeOf = typeOfMedicine;
@viraj19r
viraj19r / stringReverse.dart
Created June 24, 2019 16:36
Reverse a string using dart
void main() {
String name = "virajz";
List<int> chars = name.runes.toList();
print(chars);
for(int i = chars.length-1, j=0 ; i >= chars.length/2 ;i--, j++){
var temp = chars[i]; // i=4, j // i=3, a // i=2, j=2
chars[i] = chars[j]; // j=0, [virav] // j=1 [jiriv]
chars[j] = temp; // [jirav] // [jariv]
if(i == chars.length/2){
break;
@viraj19r
viraj19r / Palindrome.dart
Last active June 27, 2019 04:01
To check a string is palindrome or not ?
void main() {
String name = "radar";
List<int> chars = name.runes.toList();
print(checkpallindrom(chars));
}
bool checkpallindrom(List<int> chars) {
for (int i = chars.length - 1, j = 0; i >= chars.length ~/ 2; i--, j++) {
if (chars[i] != chars[j]) return false;
@viraj19r
viraj19r / PrimeNumber.dart(second approach,time complexity reduced)
Last active June 29, 2019 11:31
Check whether the number is prime or not
import 'dart:math';
main() {
print(primeNumber(0));
}
bool primeNumber(int number) {
if (number < 2) {
return false;
}
@viraj19r
viraj19r / primenumber.dart(first approach)
Created June 29, 2019 11:30
Check whether the number is prime or not
main() {
print(primeNumber(23));
}
bool primeNumber(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i < number; i++) {
if (number % i == 0) {
@viraj19r
viraj19r / insertionSort.dart
Created July 4, 2019 02:27
Insertion sort algorithm in dart
main() {
print(insertionSort([8,9, 4, 2, 6,10,12]));
}
List<int> insertionSort(List<int> list) {
for (int j = 1; j < list.length; j++) {
int key = list[j];
int i = j - 1;
@viraj19r
viraj19r / linearSearch.dart
Created July 4, 2019 02:55
linear search using dart
main() {
print(insertionSort([8,9, 4, 2],3));
}
bool insertionSort(List<int> list,int value) {
for (int i = 0; i < list.length; i++) {
if(value==list[i]){
return true;
}
}
return false;
@viraj19r
viraj19r / mergeSort.dart
Last active July 10, 2019 10:06
merge sort algorithm using recursion in dart
main(){
print(mergeSort([2,5,4,8,9]));
}
List<int> mergeSort(List<int> list){
int n = list.length;
if(n<2){
return list;
}
int midIndexOfList = (n/2).floor();
List<int> leftList = new List(midIndexOfList);
@viraj19r
viraj19r / selectionSort.dart
Created July 8, 2019 07:10
Selection sort using dart
main() {
print(selectionSort([8, 18, 11, 44, 11, 11, 345, 222, 864, 44]));
}
List<int> selectionSort(List<int> list) {
for (int j = 0; j < list.length - 1; j++) {
int min = j;
for (int i = j + 1; i < list.length; i++) {
if (list[i] < list[j]) {
min = i;
@viraj19r
viraj19r / bubbleSort.dart
Created July 8, 2019 09:51
Bubble sort algorthm using dart
main() {
print(bubbleSort([8, 4, 6, 5]));
}
List<int> bubbleSort(List<int> list) {
for (int j = 1; j < list.length; j++) {
int x = 0;
for (int i = 0; i < list.length - j; i++) {
if (list[i] > list[i + 1]) {
int key = list[i + 1];