Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project;
- import java.util.*;
- public class Sorts {
- public static void main(String[] args){
- int[] arr = {2, 9, 8,6, 7, 1, 3, 5, 4, 10};
- bubbleSort(arr);
- //selectionSort(arr);
- System.out.println(Arrays.toString(arr));
- }
- public static int[] bubbleSort(int[] arr){
- int counter = 0;
- boolean swapped = false;
- do {
- swapped = false;
- for (int i = 0; i < arr.length - 1; i++) {
- if (arr[i] > arr[i+1]){
- int temp = arr[i];
- arr[i] = arr[i + 1];
- arr[i + 1] = temp;
- swapped = true;
- }
- counter++;
- }
- } while(swapped == true);
- System.out.println(counter);
- return arr;
- }
- public static int[] selectionSort(int[] arr){
- int counter = 0;
- for (int i = 0; i < arr.length - 1; i++){
- for (int j = 0 ; j < arr.length - 1; j++){
- if (arr[j] > arr[j+1]){
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- counter++;
- }
- }
- System.out.println(counter);
- return arr;
- }
- }
Add Comment
Please, Sign In to add comment