-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbubble.c
More file actions
126 lines (103 loc) · 3.16 KB
/
Copy pathbubble.c
File metadata and controls
126 lines (103 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Program: Bubble Sort (Alternative Implementation)
* Description: Sorts an array using bubble sort with reverse iteration
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/DATA-STRUCTURES-AND-DATA-STRUCTURES-LAB
*/
#include <stdio.h>
#include <conio.h>
#define MAX_SIZE 30
// Function prototypes
int readArray(int arr[]);
void bubbleSort(int arr[], int count);
void displayArray(int arr[], int count, const char* message);
int main() {
int numbers[MAX_SIZE];
int count;
printf("=== Bubble Sort (Alternative Implementation) ===\n\n");
// Read array from user
count = readArray(numbers);
// Display original array
displayArray(numbers, count, "Original elements");
// Perform bubble sort
bubbleSort(numbers, count);
// Display sorted array
displayArray(numbers, count, "Sorted elements");
getch();
return 0;
}
/*
* Function: readArray
* Description: Reads array elements from user input
* Parameters: arr - Array to store elements
* Returns: Number of elements entered
*/
int readArray(int arr[]) {
int count, i;
printf("How many numbers are you going to enter? (max %d): ", MAX_SIZE);
scanf("%d", &count);
// Validate count
if (count > MAX_SIZE || count <= 0) {
printf("Invalid count! Using default of 5.\n");
count = 5;
}
printf("\nEnter %d numbers:\n", count);
for (i = 0; i < count; i++) {
printf(" Number %d: ", i + 1);
scanf("%d", &arr[i]);
}
return count;
}
/*
* Function: bubbleSort
* Description: Sorts array in ascending order using bubble sort
* Parameters:
* arr - Array to sort
* count - Number of elements
* Algorithm:
* - Uses reverse outer loop (count-2 down to 0)
* - Compares adjacent elements
* - Swaps if they are in wrong order
* - After each pass, largest unsorted element moves to its final position
*/
void bubbleSort(int arr[], int count) {
int i, j, temp;
printf("\n--- Sorting Process ---\n");
// Outer loop: runs from count-2 down to 0
// Each iteration fixes one element at the end
for (i = count - 2; i >= 0; i--) {
// Inner loop: compare adjacent elements
// j goes from 0 to i (unsorted portion)
for (j = 0; j <= i; j++) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap if they are in wrong order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
// Display array after each pass
printf("After pass %d: ", count - 1 - i);
for (int k = 0; k < count; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
/*
* Function: displayArray
* Description: Displays array elements with a message
* Parameters:
* arr - Array to display
* count - Number of elements
* message - Description message
*/
void displayArray(int arr[], int count, const char* message) {
int i;
printf("\n%s: ", message);
for (i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}