Skip to content

Commit 35906e7

Browse files
Address calculation sort in Java
1 parent 5113dac commit 35906e7

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

sorting/AddressCalcSort.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class AddressCalcSort
9+
{
10+
public static void sort(int[] a, int n)
11+
{
12+
int i,j,x;
13+
14+
SortedLinkedList[] List = new SortedLinkedList[6];
15+
for(i=0; i<6; i++)
16+
List[i] = new SortedLinkedList();
17+
18+
int large=0;
19+
for(i=0; i<n; i++)
20+
{
21+
if(a[i] > large)
22+
large = a[i];
23+
}
24+
25+
for(i=0; i<n; i++)
26+
{
27+
x=hash(a[i],large);
28+
List[x].insertInOrder(a[i]);
29+
}
30+
31+
/*Elements of linked lists are copied to array*/
32+
Node p;
33+
i=0;
34+
for(j=0; j<=5; j++)
35+
{
36+
p=List[j].getStart();
37+
while(p!=null)
38+
{
39+
a[i++]=p.info;
40+
p=p.link;
41+
}
42+
}
43+
}
44+
45+
public static int hash(int x, int large)
46+
{
47+
float temp;
48+
temp=(float)x/large;
49+
return (int)(temp*5);
50+
}
51+
52+
public static void main(String[] args)
53+
{
54+
int i,n;
55+
int[] a = new int[20];
56+
Scanner scan = new Scanner(System.in);
57+
58+
System.out.print("Enter the number of elements : ");
59+
n = scan.nextInt();
60+
61+
for(i=0; i<n; i++)
62+
{
63+
System.out.print("Enter element " + (i+1) + " : ");
64+
a[i] = scan.nextInt();
65+
}
66+
67+
sort(a,n);
68+
69+
System.out.println("Sorted array is : ");
70+
for(i=0; i<n; i++)
71+
System.out.print(a[i] + " ");
72+
System.out.println();
73+
scan.close();
74+
}
75+
}

0 commit comments

Comments
 (0)