- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to calculate the time of sorting an array
To calculate the time of sorting an array, let us first create an array and add elements to it −
int[] arr = new int[1000]; for (int i = 0; i < arr.length; i++) { arr[i] = (int)(i + 20); }
Now, set tow Date variables i.e. for past and future to get the time of sorting. Between both the dates, use sort() to sort the array −
Date past = new Date(); Arrays.sort(arr); Date future = new Date();
Get the difference to calculate the time of sorting −
(future.getTime() - past.getTime()
Example
import java.util.Arrays; import java.util.Date; public class Demo { public static void main(String[] args) { int[] arr = new int[1000]; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (i + 20); } Date past = new Date(); Arrays.sort(arr); Date future = new Date(); System.out.println("Time (milliseconds) = " + (future.getTime() - past.getTime())); } }
Output
Time (milliseconds) = 2
- Related Articles
- PHP program to calculate the total time given an array of times
- Java Program to Calculate the Execution Time of Methods
- Program to calculate Bitonicity of an Array
- C++ Program to calculate Bitonicity of an Array
- Sorting an array of objects by an array JavaScript
- Java Program to double the size of an array
- Alternative sorting of an array in JavaScript
- Sorting an array of binary values - JavaScript
- C Program to calculate the Round Trip Time (RTT)
- Python Program to calculate the Round Trip Time (RTT)
- Java Program to extend the size of an Integer array
- Java program to calculate the percentage
- Java program to calculate the average of numbers in Java
- Java program to reverse an array
- Java Program for Pancake sorting

Advertisements