-
May 12th, 2017, 06:02 AM
#1
How to do Shell sorting (Java SE)
I have a task, I need to compile a library catalog where there is a Structure: the name of the reader, the cipher of the book, the date of return. It is necessary to show the full name of readers who did not return the book on time (2 weeks). The task is solved using the Shell sorting method. Help if you can, sorry for my english
-
May 12th, 2017, 06:37 AM
#2
Re: How to do Shell sorting (Java SE)
I wrote the code for sorting, but I do not know how to use this code to complete the job
Code:
import java.util.Random;
import java.util.Scanner;
public class Lab {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tmp, j, i, n, d, ch;
System.out.print("n=");
n = sc.nextInt();
int matrix[] = new int[n];
System.out.print("Array:");
Random Rand = new Random();
for (i = 0; i < n; i++) {
matrix[i] = Rand.nextInt(n);
System.out.printf("%5d", matrix[i]);
}
d = n;
d = d / 2;
while (d > 0) {
for (i = 1; i < n - d; i++) {
j = i;
while (j > 0 && matrix[j] > matrix[j + d]) {
ch = matrix[j];
matrix[j] = matrix[j + d];
matrix[j + d] = ch;
j--;
}
}
d = d / 2;
}
System.out.println();
for (i = 1; i < n; i++) {
System.out.print(matrix[i]);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|