Skip to content

Commit

Permalink
DwarfPorter#1,2 task
Browse files Browse the repository at this point in the history
3 запутался, не успел разобраться до окнца.
  • Loading branch information
madbulok committed Oct 25, 2020
1 parent cb230f8 commit 375b574
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Lesson3/Algorithms/homework/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package lesson3;
import java.util.Arrays;

public class MainClass {

public static void main(String[] args) {
// Задача №1
int[] unsorted = {9,3,6,8,4,2,3,9,56,3,2,67,9,4};
AlghoritmUtils.insertSort(unsorted);
System.out.println(Arrays.toString(unsorted));

// Задача №2
AlghoritmUtils.findFibonachi(20);

}

}

class AlghoritmUtils {

static void insertSort(int[] array){
for (int i = 0; i < array.length;i++){
for (int j = 0; j < array.length; j++) {
if (array[i] < array[j]){
swapElements(array, j, i);
}
}
}
}

private static void swapElements(int[] array, int j, int i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}

// без сохранения, только вывод данныхб сложность ~O(n)
static void findFibonachi(int toNum){
int prev = 0;
int curr = 1;
System.out.println(prev);
System.out.println(curr);

int i = 2;
while (i <= toNum){
int next = prev + curr;
System.out.println(next);
prev = curr;
curr = next;
i++;
}
}
}

0 comments on commit 375b574

Please sign in to comment.