-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path03_sort_array.c
59 lines (47 loc) · 1.31 KB
/
03_sort_array.c
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
// // Write a function to sort an array of int type values using pointers. [ void sort(int *ptr,int size); ]
// // Header Files
#include <stdio.h>
#include <conio.h>
#define ARRAY_SIZE 10
// // Functions Declarations (Prototypes)
void sort(int *, int);
// // Main Function Start
int main()
{
int nums[ARRAY_SIZE];
printf("\nEnter 10 Numbers => ");
// // Input Numbers
for (int i = 0; i < ARRAY_SIZE; i++)
scanf("%d", &nums[i]);
// // Print Numbers
puts("\n>>>>>>>> Numbers Before Sorting <<<<<<<<<");
for (int i = 0; i < ARRAY_SIZE; i++)
printf("%d ", nums[i]);
// // sort array
sort(nums, ARRAY_SIZE);
// // Print Numbers
puts("\n\n>>>>>>>> Numbers After Sorting <<<<<<<<<");
for (int i = 0; i < ARRAY_SIZE; i++)
printf("%d ", nums[i]);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Function Definitions
// // Function to Sort an Array (Bubble Sort)
void sort(int *ptr, int size)
{
for (int i = 0; i < ARRAY_SIZE - 1; i++)
{
for (int j = 0; j < ARRAY_SIZE - 1 - i; j++)
{
if (*(ptr + j) > *(ptr + j + 1)) // // true, then swap
{
int temp = *(ptr + j);
*(ptr + j) = *(ptr + j + 1);
*(ptr + j + 1) = temp;
}
}
}
}