Page 288 - Computer science 868 Class 12
P. 288
9.3.2 Sorting
Sorting is the process of arranging the elements either in ascending or descending order. After the code of sorting is
executed, the elements of the array are placed in such an order, that they lie from small to big or from big to small.
According to the syllabus, there are three types of sorting:
1. Bubble sort
2. Selection sort
3. Insertion sort
Bubble Sort
Program 6 Write a program to sort a given array using bubble sort.
1 class BubbleSort
2 {
3 int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
4 void bubbleSort()
5 {
6 int i,j,n,temp;
7 n = arr.length;
8 for (i = 0; i < n - 1; i++)
9 for (j = 0; j < n - i - 1; j++)
10 if (arr[j] > arr[j + 1])
11 {
12
13 temp = arr[j];
14 arr[j] = arr[j + 1];
15 arr[j + 1] = temp;
16 }
17 }
18
19 void printArray()
20 {
21 int n,i;
22 n = arr.length;
23 for (i = 0; i < n; ++i)
24 System.out.print(arr[i] + " ");
25 System.out.println();
286286 Touchpad Computer Science-XII

