-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathaddress-calculation-sort.c
110 lines (90 loc) · 1.64 KB
/
address-calculation-sort.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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct node
{
int info ;
struct node *link;
};
struct node *start[6];
void addrCalSort(int a[], int n);
void insertInList(int num,int addr);
int hash_fn(int x, int large);
main()
{
int i,a[MAX],n;
printf("Enter the number of elements in the list : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&a[i]);
}
addrCalSort(a,n);
printf("Sorted list is :\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
void addrCalSort(int a[], int n)
{
int i,j,addr,large=0;
struct node *p;
for(i=0; i<=5; i++)
start[i]=NULL;
for(i=0; i<n; i++)
{
if(a[i] > large)
large = a[i];
}
for(i=0; i<n; i++)
{
addr=hashFn(a[i],large);
insertInList(a[i],addr);
}
/*Elements of linked lists are copied to array*/
i=0;
for(j=0; j<=5; j++)
{
p=start[j];
while(p!=NULL)
{
a[i++]=p->info;
p=p->link;
}
}
}/*End of addrCalSort()*/
int hashFn(int x, int large)
{
int addr;
float temp;
temp=(float)x/large;
addr=temp*5;
return addr;
}
/*Insertion in sorted linked list*/
void insertInList(int num,int addr)
{
struct node *q,*temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->info = num;
/*list is empty or item to be added in beginning */
if(start[addr] == NULL || num < start[addr]->info)
{
temp->link=start[addr];
start[addr]=temp;
return;
}
else
{
q=start[addr];
while(q->link != NULL && q->link->info < num)
q=q->link;
temp->link=q->link;
q->link=temp;
}
}