• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Java: Randomly sort values in a array (the generic way) in single pass

Java: Randomly sort values in a array (the generic way) in single pass

April 12, 2009 by Sze Hau Leave a Comment

Few years back (yes, about 4 years back), I wrote an article about randomly sort query results in MySQL. The main problem in randomly sort query results is that when you have a very large data in your database and the statement to sort the result may change the data execution plan causing DMBS does not choose the right plan. Therefore, sometime it is more wise to sort it in your own program instead of too depending on DBMS’s decision which may affect the overall performance of a program.

Instead of sorting the result in SQL statement, you can do it the programatic way. The pseudocode is as the following:

Load the results into an array
for each element in the array
  calculate a random number with index range from 0 to array size -1
  store the current element in a temporary variable
  replace the current element in the array with the element at the random index just now
  replace the element at random index with the element at temporary variable
end for

The code in Java programming language:

import java.util.Random;

public class SortUtil {
    public static  void sort(K[] values) {
        int i;
        int rand;
        K temp;
        Random random;

        random = new Random(System.currentTimeMillis());
        for (i = 0; i < values.length; i++) {
            rand = (random.nextInt() & 0x7FFFFFFF) % values.length;
            temp = values[i];
            values[i] = values[rand];
            values[rand] = temp;
        }
    }
}

To test the function:

...
    public static void main(String[] args) {
        Integer[] test = { 1, 2, 3, 4, 5, 6, 7, 8 , 9, 0 };

        SortUtil.sort(test);
        for(Integer i: test) {
            System.out.println("i = " + i.toString());
        }
    }
...

More from my site

  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Java: Loading Large Data into JTable or JListJava: Loading Large Data into JTable or JList
  • Java: Format integer into number of decimal places and performanceJava: Format integer into number of decimal places and performance
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer
  • Java: Continuously Read Data From FileChannel Without MappedByteBufferJava: Continuously Read Data From FileChannel Without MappedByteBuffer

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: how to, Java, performance, sorting

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2023 · Magazine Pro Theme on Genesis Framework · WordPress · Log in