KEMBAR78
Sorting Ex | PDF | Applied Mathematics | Computer Programming
0% found this document useful (0 votes)
8 views6 pages

Sorting Ex

Uploaded by

yatakonakiran2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Sorting Ex

Uploaded by

yatakonakiran2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here's a simple step-by-step explanation and a C program for sorting an array of

numbers in ascending order using the Bubble Sort algorithm:

Step-by-Step Explanation
Understand the Problem:
You need to sort a list of numbers (e.g., {5, 2, 9, 1}) in ascending order (e.g.,
{1, 2, 5, 9}).

Choose a Sorting Algorithm:


We'll use Bubble Sort, which works by repeatedly swapping adjacent elements if they
are in the wrong order.

Write the Steps of the Algorithm:

Compare each pair of adjacent elements in the array.


Swap them if the first element is greater than the second.
Repeat the process for the entire array multiple times until no swaps are needed.
Translate the Algorithm into Code.

C Program for Sorting Numeric Information (Bubble Sort)


c
Copy code
#include <stdio.h>

void bubbleSort(int arr[], int n) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int n, i;

// Step 1: Input the size of the array


printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// Step 2: Input the elements of the array


printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Step 3: Sort the array
bubbleSort(arr, n);

// Step 4: Output the sorted array


printf("Sorted array in ascending order:\n");
printArray(arr, n);

return 0;
}
Program Explanation
Input Section:

Ask the user for the number of elements (n).


Read n elements into the array.
Sorting Section:

Use nested loops in the bubbleSort function to compare and swap elements.
Output Section:

Use the printArray function to display the sorted array.


Example Run
Input:

mathematica
Copy code
Enter the number of elements: 5
Enter the elements of the array:
5 2 9 1 6
Output:

sql
Copy code
Sorted array in ascending order:
1 2 5 6 9
This program demonstrates sorting with numeric information. A similar approach can
be extended for strings using strcmp() for comparisons.

You said:
give me for string also
ChatGPT said:
ChatGPT
Here is a C program for sorting an array of strings (lexicographical order) using
the Bubble Sort algorithm.

Step-by-Step Explanation
Understand the Problem:
You need to sort a list of strings (e.g., {"banana", "apple", "orange"}) in
lexicographical (alphabetical) order (e.g., {"apple", "banana", "orange"}).

Choose a Sorting Algorithm:


We'll again use Bubble Sort for sorting strings. The idea is to compare two
adjacent strings and swap them if they are in the wrong order.
Write the Steps of the Algorithm:

Compare each pair of adjacent strings using strcmp().


Swap them if the first string is lexicographically greater than the second.
Repeat the process for the entire array multiple times until no swaps are needed.
Translate the Algorithm into Code.

C Program for Sorting String Information (Bubble Sort)


c
Copy code
#include <stdio.h>
#include <string.h>

void bubbleSort(char arr[][100], int n) {


int i, j;
char temp[100];

for (i = 0; i < n - 1; i++) {


for (j = 0; j < n - i - 1; j++) {
// Compare adjacent strings using strcmp
if (strcmp(arr[j], arr[j + 1]) > 0) {
// Swap strings if they are in the wrong order
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
}

void printArray(char arr[][100], int n) {


for (int i = 0; i < n; i++) {
printf("%s ", arr[i]);
}
printf("\n");
}

int main() {
int n, i;

// Step 1: Input the number of strings


printf("Enter the number of strings: ");
scanf("%d", &n);

char arr[n][100];

// Step 2: Input the strings


printf("Enter the strings:\n");
for (i = 0; i < n; i++) {
scanf("%s", arr[i]);
}

// Step 3: Sort the strings


bubbleSort(arr, n);

// Step 4: Output the sorted strings


printf("Sorted strings in lexicographical order:\n");
printArray(arr, n);
return 0;
}
Program Explanation
Input Section:

Ask the user for the number of strings (n).


Read n strings into the array.
Sorting Section:

Use strcmp() to compare strings. If arr[j] > arr[j+1], swap them using strcpy().
Output Section:

Use the printArray function to display the sorted strings.


Example Run
Input:

mathematica
Enter the number of strings: 3
Enter the strings:
banana apple orange
Output:

Sorted strings in lexicographical order:


apple banana orange
This program demonstrates sorting with string information. The strings are sorted
in lexicographical order using the Bubble Sort algorithm.

You said:
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
} give me indetail
ChatGPT said:
ChatGPT
The Bubble Sort algorithm is a simple sorting technique that repeatedly compares
adjacent elements in an array and swaps them if they are in the wrong order. The
process is repeated until the entire array is sorted.

Let's break down the bubbleSort function you've posted in detail:

Function: bubbleSort(int arr[], int n)


1. Function Parameters:
int arr[]: This is the array that you want to sort. It's passed by reference,
meaning any changes made inside the function will reflect in the original array.
int n: This is the number of elements in the array arr. It determines how many
times the sorting process needs to occur to ensure the entire array is sorted.
2. Variables:
int i, j: These are loop variables used to iterate through the array.
int temp: This temporary variable is used to store an element during the swapping
process.
3. Outer Loop (for (i = 0; i < n - 1; i++)):
This loop controls how many times the process of checking and swapping needs to be
repeated. The loop runs n - 1 times because in the worst case, after n - 1
iterations, the array will be sorted.
Each iteration of this loop ensures that at least one element (the largest in the
unsorted portion) is placed in its correct position.
4. Inner Loop (for (j = 0; j < n - i - 1; j++)):
This inner loop compares adjacent elements. The range of the inner loop decreases
with each pass of the outer loop because after each pass, the largest unsorted
element is "bubbled" to its correct position at the end of the array.
For example, in the first pass of the outer loop, the inner loop compares the
elements from the beginning to the second-to-last element. On the second pass, it
compares from the beginning to the third-to-last element, and so on.
5. Comparison and Swap (if (arr[j] > arr[j + 1])):
Inside the inner loop, the algorithm checks whether the current element arr[j] is
greater than the next element arr[j + 1].
If this condition is true, the elements are in the wrong order and need to be
swapped.
6. Swapping:
The swap operation is performed using a temporary variable temp:
First, the current element arr[j] is saved in temp.
Then, the next element arr[j + 1] is assigned to arr[j].
Finally, the value stored in temp (which is the original arr[j]) is assigned to
arr[j + 1].
Detailed Flow
First Pass (Outer loop, i = 0):

The inner loop runs n - 1 times, comparing adjacent elements from index 0 to n-2
(because arr[j+1] is accessed, so j goes up to n-2).
After the first pass, the largest element will "bubble" to the end of the array.
Second Pass (Outer loop, i = 1):

The inner loop runs n - 2 times, comparing adjacent elements from index 0 to n-3.
The second-largest element is now placed in its correct position.
Continue Until Sorted:

The process continues, with each pass ensuring that the next largest element is
placed at the end of the unsorted portion of the array.
Termination:

After n - 1 passes, the array will be fully sorted. The condition for stopping the
outer loop is i < n - 1, which ensures the algorithm stops when all elements are
sorted.
Example Walkthrough
Consider an array arr = {5, 3, 8, 4, 2} with n = 5.

First Pass (i = 0):


Compare 5 and 3, since 5 > 3, swap them: {3, 5, 8, 4, 2}
Compare 5 and 8, no swap needed: {3, 5, 8, 4, 2}
Compare 8 and 4, since 8 > 4, swap them: {3, 5, 4, 8, 2}
Compare 8 and 2, since 8 > 2, swap them: {3, 5, 4, 2, 8}
Now, the largest element 8 is correctly placed at the last position.
Second Pass (i = 1):
Compare 3 and 5, no swap needed: {3, 5, 4, 2, 8}
Compare 5 and 4, since 5 > 4, swap them: {3, 4, 5, 2, 8}
Compare 5 and 2, since 5 > 2, swap them: {3, 4, 2, 5, 8}
Now, the second-largest element 5 is placed in its correct position.

Third Pass (i = 2):


Compare 3 and 4, no swap needed: {3, 4, 2, 5, 8}
Compare 4 and 2, since 4 > 2, swap them: {3, 2, 4, 5, 8}
Now, the third-largest element 4 is placed in its correct position.

Fourth Pass (i = 3):


Compare 3 and 2, since 3 > 2, swap them: {2, 3, 4, 5, 8}
Finally, the array is sorted.

Time Complexity
Best case (array is already sorted): The time complexity is O(n) because in this
case, the algorithm only makes one pass through the array and performs no swaps.
Average and worst case (unsorted array): The time complexity is O(n^2) due to the
nested loops.
Space Complexity
Space complexity: O(1) because the algorithm sorts the array in-place and only
requires a constant amount of additional space (temp for swapping).
This detailed explanation should help you understand how the Bubble Sort algorithm
works and how the provided function implements it step-by-step.

You might also like