0% found this document useful (0 votes)
33 views1 page

Import Public Class Public Static Void Int

This document contains code for three sorting algorithms: insertion sort, selection sort, and bubble sort. It defines a Principal class with a main method that initializes an integer array, calls bubbleSort to sort it, and prints the sorted array. The insertionSort, selectionSort, and bubbleSort methods implement the respective sorting algorithms on an integer array.

Uploaded by

Leonan Oliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views1 page

Import Public Class Public Static Void Int

This document contains code for three sorting algorithms: insertion sort, selection sort, and bubble sort. It defines a Principal class with a main method that initializes an integer array, calls bubbleSort to sort it, and prints the sorted array. The insertionSort, selectionSort, and bubbleSort methods implement the respective sorting algorithms on an integer array.

Uploaded by

Leonan Oliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Principal.

java

1 import java.util.Arrays;
2
3 public class Principal {
4
5 public static void main(String[] args) {
6 int[] vetor = { 10,8, 2, 4, 9, 3, 6 };
7 bubbleSort(vetor);
8 System.out.println(Arrays.toString(vetor));
9 }
10
11 private static void insertionSort(int[] vetor) {
12 int aux, j;
13
14 for (int i = 1; i < vetor.length; i++) {
15 aux = vetor[i];
16 j = i - 1;
17 while ((j >= 0) && vetor[j] > aux) {
18 vetor[j + 1] = vetor[j];
19 j = j - 1;
20 }
21 vetor[j+1] = aux;
22 }
23 }
24
25 private static void selectionSort(int[] vetor) {
26 int menor, aux;
27
28 for(int i=0; i<vetor.length; i++) {
29 menor = i;
30 for(int j = i+1; j<vetor.length; j++) {
31 if(vetor[j]<vetor[menor]) {
32 menor = j;
33 }
34 }
35 aux = vetor[menor];
36 vetor[menor] = vetor[i];
37 vetor[i] = aux;
38 }
39 }
40
41 private static void bubbleSort(int[] vetor) {
42 int aux;
43 for (int i = 0; i < vetor.length - 1; i++) {
44 for (int j = 0; j < vetor.length - i - 1; j++) {
45 if (vetor[j] > vetor[j + 1]) {
46 aux = vetor[j + 1];
47 vetor[j + 1] = vetor[j];
48 vetor[j] = aux;
49 }
50 }
51 }
52 }
53
54 }
55

Page 1

You might also like