Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class sorts {
- public static void main(String[] args) {
- int num;
- int temp ;
- Scanner input = new Scanner(System.in);
- System.out.println("Enter the number of integers to sort:");
- num = input.nextInt();
- int array[] = new int[num];
- System.out.println("Enter " + num + " integers: ");
- for (int i = 0; i < num; i++)
- array[i] = input.nextInt();
- for (int i = 0; i < (num - 1); i++) { //Bubble sort
- for (int j = 0; j < num - i - 1; j++) {
- if (array[j] > array[j + 1]) {
- temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- System.out.println("Sorted list of integers (ascending)(even):");//Bubble sort
- for (int i = 0; i < num; i++)
- if (array[i] % 2 == 0) {
- System.out.println(array[i]);
- }
- for (int i = 1; i < array.length; ++i) { //Insertion sort
- temp = array[i];
- int j = i - 1;
- while (j >= 0 && array[j] < temp) {
- array[j + 1] = array[j];
- j = j - 1;
- }
- array[j + 1] = temp;
- }
- System.out.println("Sorted list of integers(descending)(odd):");//Insertion sort
- for (int i = 0; i < num; i++)
- if (array[i] % 2 != 0) {
- System.out.println(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement