Skip to content

Commit bc1c15e

Browse files
committed
Change SelectionSort.c format
1 parent 326cf62 commit bc1c15e

File tree

1 file changed

+62
-87
lines changed

1 file changed

+62
-87
lines changed

sorting/SelectionSort.c

Lines changed: 62 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,64 @@
11
//sorting of linked list using selection sort
2-
#include<stdio.h>
3-
struct node
4-
{
5-
int info;
6-
struct node *link;
7-
};
8-
struct node *start=NULL;
9-
//func to create node
10-
struct node *createnode()
11-
{
12-
struct node *p;
13-
p=(struct node*)malloc(sizeof(struct node));
14-
return(p);
2+
#include <stdio.h>
3+
4+
/*Displays the array, passed to this method*/
5+
void display(int arr[], int n){
6+
7+
int i;
8+
for(i = 0; i < n; i++){
9+
printf("%d ", arr[i]);
10+
}
11+
12+
printf("\n");
13+
1514
}
16-
//program to insert at begining
17-
void insert()
18-
{struct node *t;
19-
t=createnode();
20-
printf("\nenter the value to insert");
21-
scanf("%d",&t->info);
22-
if(start==NULL)
23-
{start=t;
24-
}
25-
else
26-
{strutc node *p;
27-
p=start;
28-
t->link=p;
29-
start=t;
30-
}
31-
//program to sort the linked list using selection sort
32-
void sort()
33-
{
34-
struct node *p,*t;
35-
t=start;
36-
int tmp;
37-
for(t=start;t->link!=NULL;t=t->link)
38-
{
39-
for(p=t->link;p!=NULL;p=p->link)
40-
{
41-
if(t->info>p->info)
42-
tmp=t->info;
43-
t->info=p->info;
44-
p->info=tmp;
45-
}
46-
}
47-
//program to view sorted list
48-
void viewlist()
49-
{
50-
struct node *p;
51-
if(start==NULL)
52-
{
53-
printf("\nlist is empty");
54-
}
55-
else
56-
{
57-
p=start;
58-
while(p!=NULL)
59-
{
60-
printf("%d",p->info);
61-
p=p->link;
62-
}
63-
}
64-
int main()
65-
{
66-
int n;
67-
whhile(1)
68-
{
69-
printf("\n1.insert value at beg");
70-
printf("\n2.sort the list");
71-
printf("\n3.view value");
72-
printf("\nenter your choice");
73-
scanf("%d",&n);
74-
switch(n)
75-
{case 1:
76-
insert();
77-
break;
78-
case 2:
79-
sort();
80-
break;
81-
case 3:
82-
viewlist();
83-
break;
84-
default:
85-
printf("\ninvalid choice");
86-
}
87-
}
88-
return(0);
89-
}
15+
16+
/*Swap function to swap two values*/
17+
void swap(int *first, int *second){
18+
19+
int temp = *first;
20+
*first = *second;
21+
*second = temp;
22+
23+
}
24+
25+
/*This is where the sorting of the array takes place
26+
arr[] --- Array to be sorted
27+
size --- Array Size
28+
*/
29+
void selectionSort(int arr[], int size){
30+
31+
for(int i=0; i<size; i++) {
32+
int min_index = i;
33+
for(int j=i+1; j<size; j++) {
34+
if(arr[min_index] > arr[j]) {
35+
min_index = j;
36+
}
37+
}
38+
swap(&arr[i], &arr[min_index]);
39+
}
40+
}
41+
42+
int main(int argc, const char * argv[]) {
43+
int n;
44+
printf("Enter size of array:\n");
45+
scanf("%d", &n); // E.g. 8
46+
47+
printf("Enter the elements of the array\n");
48+
int i;
49+
int arr[n];
50+
for(i = 0; i < n; i++){
51+
scanf("%d", &arr[i] );
52+
}
53+
54+
printf("Original array: ");
55+
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
56+
57+
selectionSort(arr, n);
58+
59+
printf("Sorted array: ");
60+
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
61+
62+
return 0;
63+
}
64+

0 commit comments

Comments
 (0)