Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Q 25 Bubble sort
- public class Main
- {
- public static void main(String[] args) {
- int[] arr = {50,4,31,2,-3};
- int temp,flag;
- int n=5; // size of array
- for (int i = 0; i < n - 1; i++)
- {
- flag = 0;
- for (int j = 0; j < n - i - 1; j++)
- {
- if (arr[j] > arr[j + 1])
- {
- temp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = temp;
- flag = 1;
- }
- }
- if (flag==0)
- break;
- }
- for (int i = 0; i < n; i++)
- {
- System.out.print(arr[i]+" ");
- }
- }
- }
- /*
- Input 50,4,31,2,-3
- Output -3,2,4,31,50
- */
- Q 26 Insertion sort
- public class Main
- {
- static void InsertionSort(int arr[],int n)
- {
- for (int i = 1; i < n; ++i) {
- int key = arr[i];
- int j = i - 1;
- while (j >= 0 && arr[j] > key) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- }
- }
- public static void main(String[] args) {
- int[] arr = {5,4,3,2,1};
- int n=5; // size of array
- InsertionSort(arr,n);
- for (int i = 0; i < n; i++)
- {
- System.out.print(arr[i]+" ");
- }
- }
- }
- /*
- Input 5,4,3,2,1
- Output 1 2 3 4 5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement