8000 Selection Sort Updated · sbccas/c-programming-tutorials@34b2c36 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34b2c36

Browse files
Selection Sort Updated
1 parent a809661 commit 34b2c36

11 files changed

+62
-16
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"CP5ArrayDemoLab.C": "cpp",
2828
"CP5ArraySortingFunction.C": "cpp",
2929
"PAP81ArrayMatrixDemo.C": "cpp",
30-
"CP5SimpleStringDemo.C": "cpp"
30+
"CP5SimpleStringDemo.C": "cpp",
31+
"CP5ArraySortDemoDeleteme.C": "cpp"
3132
}
3233
}

2_Notes/Bubble_Sort_Algorithm.pdf

442 KB
Binary file not shown.

3_Programs/CP5ArraySelectionSort.C

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include<conio.h>
77
int main()
88
{
9-
int n, i, j, array[50], sml,temp;
9+
int n, i, j,k, array[50], sml,temp;
1010
printf("\nEnter Size for an Array (< 50): ");
1111
scanf("%d", &n);
1212
for (i = 0; i < n; i++)
@@ -26,13 +26,25 @@ int main()
2626
sml = i;
2727
for ( j = i+1; j < n; j++)
2828
{
29-
if (array[j]<array[sml]){
29+
if (array[j]<array[sml])
30+
{
3031
sml = j;
3132
}
33+
}
34+
if (sml!=i)
35+
{
3236
temp = array[i];
3337
array[i] = array[sml] ;
3438
array[sml] = temp;
39+
}
40+
printf("\n");
41+
for (k = 0; k < n; k++)
42+
{
43+
printf("%d\t",array[k]);
3544
}
45+
46+
47+
3648
}
3749
printf("\nArray Elements after Selection Sorting : \n");
3850
for (i = 0; i < n; i++)

3_Programs/CP5ArraySelectionSort.exe

599 Bytes
Binary file not shown.

3_Programs/CP5ArraySortDemoDeleteme.C

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int fybca4[50]= {456,474,464,444,428};
5+
int i,j,n =5, temp;
6+
//PRINT UNSORTED ARRAY:
7+
printf("UNSORTED ARRAY =\n");
8+
for (i =0;i<5;i++)
9+
printf("| %d |",fybca4[i]);
10+
11+
//IMPLEMENTING BUBBLE SORT
12+
for(i=0;i < n-1 ;i++)
13+
{
14+
for(j = 0; j < n-1 -i; j++)
15+
{
16+
//check adjacent elements if left is greater than right than swap
17+
if(fybca4[j] > fybca4[j+1])
18+
{
19+
//swapping
20+
temp = fybca4[j];
21+
fybca4[j] = fybca4[j+1] ;
22+
fybca4[j+1] = temp;
23+
}
24+
25+
}
26+
}
27+
//printing sorted array
28+
printf("\nSORTED ARRAY =\n");
29+
for (i =0;i<5;i++)
30+
printf("| %d |",fybca4[i]);
31+
32+
33+
}
34+
41.4 KB
Binary file not shown.

3_Programs/CP5ArraySortingFunction.C

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,31 @@ int main()
1111
m=n;
1212
for (i = 0; i < n; i++)
1313
{
14-
printf("Enter Element for array[%d]= ", i);
14+
printf("Enter Element array[%d]= ", i);
1515
scanf("%d", &array[i]);
1616
}
17-
printf("\nElements of Inputed Array:\n");
17+
printf("\nInputed Array:\t");
1818
showdata();
1919
printf("\nApplying Bubble Sorting : \n");
2020
// CHECKING ELEMENTS AND SWAPPING IF ELEMENT IS GREATED THAN SECOND ELEMENT:
2121
for (i = 0; i < n ; i++)
2222
{
23-
printf("\nPass %d, i=%d\n",i+1,i);
23+
printf("\nPass %d, i=%d\t",i+1,i);
24+
showdata();
2425
for (j = 0; j < n-1-i; j++)
2526
{
26-
printf("\nj=%d,j+1=%d \t",j,j+1);
27+
printf("\tj=%d,j+1=%d \t",j,j+1);
2728
if (array[j] > array[j+1])
2829
{
2930
temp = array[j];
3031
array[j] = array[j+1];
3132
array[j+1] = temp;
33+
printf(" SWAPPED");
3234
}
35+
else{printf(" NO SWAP");}
36+
printf("\n\t\t");
3337
showdata();
34-
}
35-
36-
38+
}
3739

3840
}
3941
printf("\nArray Elements after Bubble Sorting : \n");
@@ -46,6 +48,6 @@ for (k = 0; k < n; k++)
4648
{
4749
printf("| %d ",array[k]);
4850
}
49-
printf("|\n");
51+
printf("|");
5052
// return;
5153
}
87 Bytes
Binary file not shown.

3_Programs/CP5SimpleStringDemo.C

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,4 @@ for (i = strlen(str1); i >= 0; i--)
2828
}
2929
//printf("\nNew String is : %d\n", str1);
3030

31-
32-
33-
34-
3531
}

3_Programs/CP5SimpleStringDemo.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0