1#include<stdio.h>
2int main(){
3 /* Here i & j for loop counters, temp for swapping,
4 * count for total number of elements, number[] to
5 * store the input numbers in array. You can increase
6 * or decrease the size of number array as per requirement
7 */
8 int i, j, count, temp, number[25];
9
10 printf("How many numbers u are going to enter?: ");
11 scanf("%d",&count);
12
13 printf("Enter %d elements: ", count);
14 // Loop to get the elements stored in array
15 for(i=0;i<count;i++)
16 scanf("%d",&number[i]);
17
18 // Logic of selection sort algorithm
19 for(i=0;i<count;i++){
20 for(j=i+1;j<count;j++){
21 if(number[i]>number[j]){
22 temp=number[i];
23 number[i]=number[j];
24 number[j]=temp;
25 }
26 }
27 }
28
29 printf("Sorted elements: ");
30 for(i=0;i<count;i++)
31 printf(" %d",number[i]);
32
33 return 0;
34}
1#include <stdio.h>
2int main()
3{
4
5 int ara[1000];
6 int high;
7 printf("How many numbers are you going to enter ? (max:1000) : ");
8 scanf("%d", &high);
9 int i, j, min;
10 printf("\n");
11 for (i = 0; i < high; i++)
12 {
13 printf("Type number : ");
14 scanf("%d", &ara[i]);
15 }
16 printf("\n The numbers arranged in ascending order are : \n");
17 for (i = 0; i < high; i++)
18 {
19 for (j = i; j < high; j++)
20 {
21 if (ara[i] > ara[j])
22 {
23 min = ara[i];
24 ara[i] = ara[j];
25 ara[j] = min;
26 }
27 }
28 // The numbers arranged in ascending order are
29 printf("%d\n", ara[i]);
30 }
31 return 0;
32}
1for (int i = 0; i < arr.length; i++) {
2 for (int j = 0; j < arr.length-1-i; j++) {
3 if(arr[j]>arr[j+1])
4 {
5 int temp=arr[j];
6 arr[j]=arr[j+1];
7 arr[j+1]=temp;
8 }
9 }
10 System.out.print("Iteration "+(i+1)+": ");
11 printArray(arr);
12 }
13 return arr;
14// BUBBLE SORT
1#include <stdio.h>
2
3int main()
4{
5 int ara[10]={4,45,23,53,2,64,56,34,77,98};
6 int i,j,temp;
7 for(i = 0; i < 10; i++){
8
9 for(j = 0; j< 10; j++){
10 if(ara[i] < ara[j]){ //use getter than (>) symbol for sorting from getter than to less than
11
12 temp = ara[i];
13 ara[i] = ara[j];
14 ara[j] = temp;
15
16 }
17 }
18
19 }
20
21 for(i = 0; i < 10; i++){ //printing array sorted array elements
22
23 printf("%d\t",ara[i]);
24 }
25 return 0;
26}
27
1#include <stdio.h>
2
3struct student
4{
5 int rollno;
6 char name[80];
7 int marks;
8};
9
10void accept(struct student list[], int s);
11void display(struct student list[], int s);
12void bsortDesc(struct student list[], int s);
13
14int main()
15{
16 struct student data[20];
17 int n;
18
19 printf("Number of records you want to enter? : ");
20 scanf("%d", &n);
21
22 accept(data, n);
23 printf("\nBefore sorting");
24 display(data, n);
25 bsortDesc(data, n);
26 printf("\nAfter sorting");
27 display(data, n);
28
29 return 0;
30}
31
32void accept(struct student list[80], int s)
33{
34 int i;
35 for (i = 0; i < s; i++)
36 {
37 printf("\n\nEnter data for Record #%d", i + 1);
38
39 printf("\nEnter rollno : ");
40 scanf("%d", &list[i].rollno);
41
42 printf("Enter name : ");
43 gets(list[i].name);
44
45 printf("Enter marks : ");
46 scanf("%d", &list[i].marks);
47 }
48}
49
50void display(struct student list[80], int s)
51{
52 int i;
53
54 printf("\n\nRollno\tName\tMarks\n");
55 for (i = 0; i < s; i++)
56 {
57 printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
58 }
59}
60
61void bsortDesc(struct student list[80], int s)
62{
63 int i, j;
64 struct student temp;
65
66 for (i = 0; i < s - 1; i++)
67 {
68 for (j = 0; j < (s - 1-i); j++)
69 {
70 if (list[j].marks < list[j + 1].marks)
71 {
72 temp = list[j];
73 list[j] = list[j + 1];
74 list[j + 1] = temp;
75 }
76 }
77 }
78}
1 #include <stdio.h>
2 void main()
3 {
4
5 int i, j, a, n, number[30];
6 printf("Enter the value of N \n");
7 scanf("%d", &n);
8
9 printf("Enter the numbers \n");
10 for (i = 0; i < n; ++i)
11 scanf("%d", &number[i]);
12
13 for (i = 0; i < n; ++i)
14 {
15
16 for (j = i + 1; j < n; ++j)
17 {
18
19 if (number[i] > number[j])
20 {
21
22 a = number[i];
23 number[i] = number[j];
24 number[j] = a;
25
26 }
27
28 }
29
30 }
31
32 printf("The numbers arranged in ascending order are given below \n");
33 for (i = 0; i < n; ++i)
34 printf("%d\n", number[i]);
35 }