Przejdź do głównej zawartości

Sorting algor

BubbleSort


// BubbleSort
        System.out.println("BubbleSort");
        for (int i=0; i<data.length-1; i++){
            for (int j=0; j<data.length-1-i;j++){
                if (data[j] > data[j+1]) {
                    int temp = data[j];
                    data[j]= data[j+1];
                    //swap
                    data[j+1]=temp;
                }
            }
        }


  • mało efektywny z uwagi na podwójny loop
  • tylko do małych data sets
  • efektywność O(n^2)


SelectionSort


  • raczej wolny algorytm z uwagi na podwójny loop
  • małe data set
  • efektywność O(n^2)


System.out.println("SelectionSort");
        int i,j,minV,minI,temp=0;
        for(i=0; i<data.length;i++){
            minV = data[i];
            minI = i;
            for (j=i;j<data.length;j++){
                if(data[j]<minV){
                    minV=data[j];
                    minI=j;
                }
            }
//swap
            if(minV<data[i]){
                temp=data[i];
                data[i]=data[minI];
                data[minI]=temp;
            }
        }

Insertion sort:

  • przesuwa key do momentu spełnienia warunku z while
  • raczej wolny algorytm z uwagi na podwójny loop
  • małe data set
  • efektywność O(n^2)


        System.out.println("InsertionSort");
        long startTimeInsertSort = System.nanoTime();

        for (int k=1;k<data.length;k++){
            int key = data[k];
            int m = k-1;
            while (m>0&& key<data[m]){
                int temp2 = data[m];
                data[m]=data[m+1];
                data[m+1]=temp;
                m--;
            }
        }

Merge sort

  • metoda sortująca rekursywna metoda wywołuje samą siebie
  • efektywna dla duzych zbiorów


Komentarze

Popularne posty z tego bloga

Stored Procedures - JDBC Java SQL

Stored Procedures: group of SQL statements that perform a particular task  Normally created by DBA Can have any combination of input, output, and input/output parameters Benefits: Performance  - compiled once but executable more times Productivity and Ease of Use  - avoid redundant code, extend SQL Database functionality  Scalability   Maintainability Interoperability Replication Security To call stored procedure from Java The JDBC API provides the CallableStatement CallableStatement myCall = myConn.prepareCall("{ call some_stored_procedure() }"); ... myCall.execute(); JDBC API parameter types IN default INOUT OUT Stored Procedure can return result sets EXAMPLE: Created stored procedure on MySQL side - NAME:  increase_salaries_for_department DELIMITER $$ DROP PROCEDURE IF EXISTS ` increase_salaries_for_department `$$ CREATE DEFINER=`student`@`localhost` PROCEDURE `increase_salaries_for_departme...

SQL - Podstawy

SELECT * FROM branch; CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... ); Typy danych w kolumnach Text data types: VARCHAR (40) - string z ograniczoną liczbą znaków TEXT - string BLOB - Binary Larg Objects - 65000 bytes of data ENUM('X','Y','Z') - list of values in order we enter them SET - values are ENUM up to 64 list items    Number data types: INT (size) - integer FLOAT(size,d) - small number with floating decimal point, d - decimal point DOUBLE(size,d) large number with floating decimal point Date data types: DATE() - YYYY-MM-DD TIMESTAMP() TIME() SQL Keywords: ADD - add column ALTER - add delete modify columns in table or change data types of column in table CREATE - create database, table, index, view, procedure CREATE DATABASE CREATE TABLE CREATE INDEX CREATE VIES DELETE - delete rows from table DESC - sort results descending order DROP - delet...

Java - variables , data types

Data Types Data types in Java are classified in to types: Primitive - which include Integer, Character, Boolean and Floating Point Non-primitive - which includes Classes, Interfaces and Arrays Integer : byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point : number + fractional parts  float double Character : stores character Boolean : hold true false value Variables  Instance Variables (non-Static Fields) -  Objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Class Variables - Static Fields - any field with static modifier says that there is only one copy of this variable in existence static int myClassVariable = 6;  Local Variables - a method stores it's temporary state of local variables int count=0; Parameters - variables which will be passed to the methods of a class