mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add sorting-algorithms/dart/bubble-sort
This commit is contained in:
parent
aa3eec06ba
commit
50a1cda3d8
@ -19,7 +19,7 @@ You are given a list of `n` array definitions and your job is to figure out what
|
|||||||
### Input
|
### Input
|
||||||
|
|
||||||
- **Line 1:** An integer `n` for the number of array assignments
|
- **Line 1:** An integer `n` for the number of array assignments
|
||||||
- **`N` next lines:** One array assignment per line: `array_identifier` [ `first_index` .. `last_index` ] = `last_index - first_index + 1` integers separated by space
|
- **`n` next lines:** One array assignment per line: `array_identifier` [ `first_index` .. `last_index` ] = `last_index - first_index + 1` integers separated by space
|
||||||
- **Line `n+2`:** Element to print: `arr` [ `i` ]
|
- **Line `n+2`:** Element to print: `arr` [ `i` ]
|
||||||
|
|
||||||
### Constraints
|
### Constraints
|
||||||
|
@ -9,6 +9,11 @@ We will use the [numerical order](https://en.wikipedia.org/wiki/Numerical_order)
|
|||||||
|
|
||||||
Write a function that takes a list of integers and sort them in ascending order.
|
Write a function that takes a list of integers and sort them in ascending order.
|
||||||
|
|
||||||
|
## Input
|
||||||
|
|
||||||
|
- **Line 1:** An integer `n` for the length of the list of integers
|
||||||
|
- **`n` next lines:** the numbers to sort
|
||||||
|
|
||||||
### Constraints
|
### Constraints
|
||||||
|
|
||||||
- List of integers length <= 25 000
|
- List of integers length <= 25 000
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
#include "bubble_sort.h"
|
#include "bubble_sort.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int *numbers = malloc(25000 * sizeof(int));
|
|
||||||
int current_number;
|
int current_number;
|
||||||
|
int length = scanf("%d", ¤t_number);
|
||||||
|
int *numbers = malloc(current_number * sizeof(int));
|
||||||
int index_input = 0;
|
int index_input = 0;
|
||||||
while (scanf("%d", ¤t_number) != EOF) {
|
while (scanf("%d", ¤t_number) != EOF) {
|
||||||
numbers[index_input] = current_number;
|
numbers[index_input] = current_number;
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
#include "insertion_sort.h"
|
#include "insertion_sort.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int *numbers = malloc(25000 * sizeof(int));
|
|
||||||
int current_number;
|
int current_number;
|
||||||
|
int length = scanf("%d", ¤t_number);
|
||||||
|
int *numbers = malloc(current_number * sizeof(int));
|
||||||
int index_input = 0;
|
int index_input = 0;
|
||||||
while (scanf("%d", ¤t_number) != EOF) {
|
while (scanf("%d", ¤t_number) != EOF) {
|
||||||
numbers[index_input] = current_number;
|
numbers[index_input] = current_number;
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
#include "merge_sort.h"
|
#include "merge_sort.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int *numbers = malloc(25000 * sizeof(int));
|
|
||||||
int current_number;
|
int current_number;
|
||||||
|
int length = scanf("%d", ¤t_number);
|
||||||
|
int *numbers = malloc(current_number * sizeof(int));
|
||||||
int index_input = 0;
|
int index_input = 0;
|
||||||
while (scanf("%d", ¤t_number) != EOF) {
|
while (scanf("%d", ¤t_number) != EOF) {
|
||||||
numbers[index_input] = current_number;
|
numbers[index_input] = current_number;
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
# sorting-algorithms/dart/bubble-sort
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 29 June 2021.
|
||||||
|
|
||||||
|
| Algorithm | Best Case | Average Case | Worst Case |
|
||||||
|
| ----------------------------------------------------------- | ----------- | ------------ | ----------- |
|
||||||
|
| [Bubble sort](https://wikipedia.org/wiki/Bubble_sort) | O(n) | O(n²) | O(n²) |
|
@ -0,0 +1,32 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int length = int.parse(readLineSync());
|
||||||
|
List<int> numbers = [];
|
||||||
|
for (int indexInput = 0; indexInput < length; indexInput++) {
|
||||||
|
numbers.add(int.parse(readLineSync()));
|
||||||
|
}
|
||||||
|
List<int> sortedNumbers = bubbleSort(numbers);
|
||||||
|
for (int index = 0; index < length; index++) {
|
||||||
|
print(sortedNumbers[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> bubbleSort(Iterable<int> numbersInput) {
|
||||||
|
var numbers = [...numbersInput];
|
||||||
|
for (var index1 = 0; index1 < numbers.length; index1++) {
|
||||||
|
for (var index2 = 0; index2 < numbers.length - index1 - 1; index2++) {
|
||||||
|
if (numbers[index2] > numbers[index2 + 1]) {
|
||||||
|
var temporary = numbers[index2];
|
||||||
|
numbers[index2] = numbers[index2 + 1];
|
||||||
|
numbers[index2 + 1] = temporary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
String readLineSync() {
|
||||||
|
String? string = stdin.readLineSync();
|
||||||
|
return string == null ? '' : string;
|
||||||
|
}
|
@ -12,7 +12,7 @@ readlineInterface.on('close', solution)
|
|||||||
|
|
||||||
function solution() {
|
function solution() {
|
||||||
const numbers = input.map((value) => Number(value))
|
const numbers = input.map((value) => Number(value))
|
||||||
const sortedNumbers = bubbleSort(numbers)
|
const sortedNumbers = bubbleSort(numbers.slice(1))
|
||||||
sortedNumbers.forEach((number) => {
|
sortedNumbers.forEach((number) => {
|
||||||
console.log(number)
|
console.log(number)
|
||||||
})
|
})
|
||||||
|
@ -12,7 +12,7 @@ readlineInterface.on('close', solution)
|
|||||||
|
|
||||||
function solution() {
|
function solution() {
|
||||||
const numbers = input.map((value) => Number(value))
|
const numbers = input.map((value) => Number(value))
|
||||||
const sortedNumbers = insertionSort(numbers)
|
const sortedNumbers = insertionSort(numbers.slice(1))
|
||||||
sortedNumbers.forEach((number) => {
|
sortedNumbers.forEach((number) => {
|
||||||
console.log(number)
|
console.log(number)
|
||||||
})
|
})
|
||||||
|
@ -12,7 +12,7 @@ readlineInterface.on('close', solution)
|
|||||||
|
|
||||||
function solution() {
|
function solution() {
|
||||||
const numbers = input.map((value) => Number(value))
|
const numbers = input.map((value) => Number(value))
|
||||||
const sortedNumbers = mergeSort(numbers)
|
const sortedNumbers = mergeSort(numbers.slice(1))
|
||||||
sortedNumbers.forEach((number) => {
|
sortedNumbers.forEach((number) => {
|
||||||
console.log(number)
|
console.log(number)
|
||||||
})
|
})
|
||||||
|
@ -18,6 +18,7 @@ numbers: List[int] = []
|
|||||||
for value in sys.stdin:
|
for value in sys.stdin:
|
||||||
numbers.append(int(value.rstrip('\n')))
|
numbers.append(int(value.rstrip('\n')))
|
||||||
|
|
||||||
|
numbers = numbers[1:]
|
||||||
sorted_numbers = bubble_sort(numbers)
|
sorted_numbers = bubble_sort(numbers)
|
||||||
for number in sorted_numbers:
|
for number in sorted_numbers:
|
||||||
print(number)
|
print(number)
|
||||||
|
@ -18,6 +18,7 @@ numbers: List[int] = []
|
|||||||
for value in sys.stdin:
|
for value in sys.stdin:
|
||||||
numbers.append(int(value.rstrip('\n')))
|
numbers.append(int(value.rstrip('\n')))
|
||||||
|
|
||||||
|
numbers = numbers[1:]
|
||||||
sorted_numbers = insertion_sort(numbers)
|
sorted_numbers = insertion_sort(numbers)
|
||||||
for number in sorted_numbers:
|
for number in sorted_numbers:
|
||||||
print(number)
|
print(number)
|
||||||
|
@ -41,6 +41,7 @@ numbers: List[int] = []
|
|||||||
for value in sys.stdin:
|
for value in sys.stdin:
|
||||||
numbers.append(int(value.rstrip('\n')))
|
numbers.append(int(value.rstrip('\n')))
|
||||||
|
|
||||||
|
numbers = numbers[1:]
|
||||||
sorted_numbers = merge_sort(numbers)
|
sorted_numbers = merge_sort(numbers)
|
||||||
for number in sorted_numbers:
|
for number in sorted_numbers:
|
||||||
print(number)
|
print(number)
|
||||||
|
@ -1 +1,2 @@
|
|||||||
|
1
|
||||||
1
|
1
|
@ -1,3 +1,4 @@
|
|||||||
|
10
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
5
|
5
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
100
|
||||||
86
|
86
|
||||||
100
|
100
|
||||||
98
|
98
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
1000
|
||||||
111
|
111
|
||||||
72
|
72
|
||||||
170
|
170
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
25000
|
||||||
1342
|
1342
|
||||||
4152
|
4152
|
||||||
19704
|
19704
|
||||||
|
Loading…
Reference in New Issue
Block a user