## Program to Sort the Elements of Array

#### Definition & Explanation

Sorting the elements of an array is a crucial operation in programming, enabling efficient data organization and retrieval. Various algorithms such as Bubble Sort, Selection Sort, and Quick Sort are employed for this purpose, each with its trade-offs in time and space complexity.

Efficient sorting algorithms like Merge Sort and Heap Sort offer better performance for larger datasets. The choice of algorithm depends on factors such as array size and desired efficiency. Sorting algorithms typically involve comparing and rearranging elements to achieve ascending or descending order. Quadratic time complexity algorithms like Bubble Sort and Insertion Sort are suitable for small arrays, while algorithms with O(n log n) complexity like Merge Sort are preferred for larger datasets. Sorting arrays is fundamental in applications like searching, optimizing data structures, and implementing efficient algorithms.

**More Description**

Program Logic

- Start with an unsorted array of elements.
- Repeat the following steps until no swaps are made during a pass through the array: a. Iterate through the array from the beginning to the second-to-last element. b. Compare adjacent elements. c. If the current element is greater than the next element, swap them.
- After completing a pass through the array, the largest element will be at the last index.
- Repeat steps 2 and 3, each time reducing the range of elements to be considered by one (exclude the last element which is already in its final sorted position).
- Continue until no swaps are made during a pass, indicating that the array is sorted.
- The array is now sorted in ascending order.

#### Method 1 :

```
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
```

#### Output :

#### Method 1 :

```
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
for (int i=0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
```

#### Output :

#### Method 1 :

```
public class BubbleSort {
void bubbleSort(int arr[]) {
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
public static void main(String args[]) {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
BubbleSort ob = new BubbleSort();
ob.bubbleSort(arr);
System.out.println("Sorted array:");
for (int i=0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
```

#### Output :

#### Method 1 :

```
number= [10,45,30,1]
number.sort()
print(number)
```

#### Output :

`[1, 10, 30, 45]`