-
Notifications
You must be signed in to change notification settings - Fork 0
/
SORT_VIN.C
152 lines (148 loc) · 3.61 KB
/
SORT_VIN.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Author : Sharma Vinay
Email : [email protected]
Date of creation : 18 july 2016
Objective : Learn Sorting of an array */
#include<stdio.h>
#include<conio.h>
#define SIZE 4
void main()
{
int a[SIZE];
int i,j,temp,k,count,choice;
char ch,input[100],output[100];
int no[26]={0},n,c,t,x;
clrscr();
printf("Enter your choice :=");
printf("\n--------------------------------------");
printf("\n1.....Ascending order sort array");
printf("\n2.....descending order sort array");
printf("\n3.....Character sort array");
printf("\n--------------------------------------\n");
scanf("%d",&choice);
switch(choice)
{
case 1: //For ascending order sort
printf("\n--------------------------------------");
for(i=0;i<SIZE;i++)
{
printf("\nEnter the value of %d array :",SIZE);
scanf("%d",&a[i]);
}//End of for loop
printf("\nThe array before sorting \n");
for(i=0;i<SIZE;i++)
{
printf("%d\t",a[i]);
}//End of for loop
for(i=0;i<SIZE;i++)
{
count=0;
printf("\n");
for(j=0;j<SIZE-1;j++)
{
if(a[j]>a[j+1])
{
count=count+1;
}//End of if
if(count ==0 )
goto exit;
}//End of for loop
printf("\n pass%d\ncnt= %d ",i+1,count);
for(j=0;j<SIZE-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}//End of if for swap
}//End of for loop
for(k=0;k<SIZE;k++)
{
printf("%2d,",a[k]);
}//End of for loop
printf("\n");
}//End of outer for loop
exit:
printf("\nArray after sorting \n");
printf("--------------------------------------\n");
for(i=0;i<SIZE;i++)
{
printf("%d\t",a[i]);
}//End of for loop
break;
//-------------------------------------------------------------------------------------------------
case 2: //For decending order sort
printf("\n--------------------------------------");
for(i=0;i<SIZE;i++)
{
printf("\nEnter the value of %d array :",SIZE);
scanf("%d",&a[i]);
}//End of for loop
printf("\nThe array before sorting \n");
for(i=0;i<SIZE;i++)
{
printf("%d\t",a[i]);
}//End of for loop
for(i=0;i<SIZE;i++)
{
count=0;
printf("\n");
for(j=0;j<SIZE-1;j++)
{
if(a[j]<a[j+1])
{
count=count+1;
}//End of for loop
if(count ==0 )
goto exit;
}//End of for loop
printf("\n pass%d\ncnt= %d ",i+1,count);
for(j=0;j<SIZE-1;j++)
{
if(a[j] < a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}//End of if for swap
}//End of for loop
for(k=0;k<SIZE;k++)
{
printf("%2d",a[k]);
}//End of for loop
printf("\n");
}//End of outer for loop
printf("\nArray after sorting ");
printf("\n--------------------------------------\n");
for(i=0;i<SIZE;i++)
{
printf("%d\t",a[i]);
}//End of for loop
break;
//--------------------------------------------------------------------------------------------------
case 3: //Character sort
printf("\n--------------------------------------");
printf("\nEnter text :\n");
scanf("%s",input);
n=strlen(input);
for(c=0;c<n;c++)
{
ch=input[c]-'a';
no[ch]++;
}//End of for loop
t=0;
for(ch='a';ch<='z';ch++)
{
x=ch-'a';
for(c=0;c<no[x];c++)
{
output[t]=ch;
t++;
}//End of inner for loop
}//End of outer for loop
output[t]='\0';
printf("\n--------------------------------------\n");
printf("%s\n",output);
}//End of choice
getch();
}//End of main