﻿ Java exercises and solutions: array sort

# Java exercises and solutions: array sort

## Java arrays

### Exercise 1: By using the bubble sort algorithm, write a Java program to sort an integer array of 10 elements in ascending.

Solution:

public class BubbleSort {

public static void main(String[] args){
//unsorted array
int[] arr={12,34,23,2,4,56,80,34,45,90};
//sorted array using bubble sort
bubblesort(arr,arr.length);
//display the content of sorted array
int i;
for(i=0;i<arr.length;i++) System.out.println(arr[i]);

}

public static void bubblesort(int[] dataset, int n){
int i,j;
for(i=0;i<n;i++)
for(j=n-1;j>i;j--)
if(dataset[j]<dataset[j-1])
{
int temp=dataset[j];
dataset[j]=dataset[j-1];
dataset[j-1]=temp;

}

}

}

### Exercise 2: Modify the Java code in exercise 1 to sort the array in descending order.

Solution:

public class BubbleSort {

public static void main(String[] args){
//unsorted array
int[] arr={12,34,23,2,4,56,80,34,45,90};
//sorted array using bubble sort
bubblesort(arr,arr.length);
//display the content of sorted array
int i;
for(i=0;i<arr.length;i++) System.out.println(arr[i]);

}

public static void bubblesort(int[] dataset, int n){
int i,j;
for(i=0;i<n;i++)
for(j=n-1;j>i;j--)
if(dataset[j]>dataset[j-1])
{
int temp=dataset[j];
dataset[j]=dataset[j-1];
dataset[j-1]=temp;

}

}

}