MTech ADSA Programs

Embed Size (px)

Citation preview

  • 8/11/2019 MTech ADSA Programs

    1/26

    1. Write a Java program for sorting a given list of names in ascending order using bubble sortalgorithm.

    class SortNames

    {public void sort(String[] a)

    {

    int i, pass, n = a.length;

    String tmp;

    for( pass = 0; pass n; pass!! )

    {

    for( i = 0; i n"pass"#; i!! )

    if( (($omparable)a[i]).compare%o(a[i!#]) & 0)

    {

    tmp = a[i]; '' Sap a[i], a[i!#]

    a[i] = a[i!#];

    a[i!#] = tmp;

    '''''''''''''''''''''''''' SortNamesDemo.java ''''''''''''''''''''

    class Sort*ames+emo

    {

    public static void main(String[] args)

    {

    Sort*ames sn = ne Sort*ames();

    String[] names =

    {-amu, ohn, /nu, ri1a, 2asha, rem;

    int i;

    System.out.println("Unsorted names:")for(i=0; i names.length; i!!)

    S1stem.out.println(names[i]);

    sn.sort(names);S1stem.out.println(Sorted names3);for(i=0; i names.length; i!!)

    S1stem.out.println(names[i]);

    }

    }

    Output of this program is:4nsorted names3

    -amu

    ohn

    /nu

    ri1a

    2asha

    rem

    Sorted names3/nu

    2asha

    ohn

    rem

    ri1a

    -amu

  • 8/11/2019 MTech ADSA Programs

    2/26

  • 8/11/2019 MTech ADSA Programs

    3/26

    Binary search(b)Binary search is a simple method of accessing a particular item in a sorted (ordered) data set. ! search for a

    particular item with a certain %ey alue resembles the search for a name in telephone directory or a word in adictionary. The approimate middle item of the data set is located, and its %ey alue is eamined. *f its alue is toohigh, then the %ey of the middle element of the first half of the set is eamined and procedure is repeated on the first

    half until the required item is found. *f the alue is too low, then the %ey of the middle entry of the second half of the

    data set is tried and the procedure is repeated on the second half. This process continues until the desired %ey is found

    or search interal becomes empty. The binary search algorithm is based on binary search tree.

    Program 2(b): Iterative Binary searchclass 2inar1Search+emo

    {

    static 6b7ect[] a = { /, A/, BC, B, 6-, %*, 4, D2;

    static 6b7ect @e1 = 4;

    public static void main(String args[])

    {

    if( binar1Search() )S1stem.out.println(@e1 ! found in the list);

    else

    S1stem.out.println(@e1 ! not found in the list);

    static boolean binar1Search()

    {

    int c, mid, lo = 0, high = a.length"#;

    hile( lo = high)

    {

    mid = (lo ! high)'>;

    c = (($omparable)@e1).compare%o(a[mid]);

    if( c 0) high = mid"#;

    else if( c & 0) lo = mid!#;

    else return true;

    return false;

    Program2(b): Recursive Binary search

    class -ecursive2inar1Search+emo

    {

    static 6b7ect[] a = { /, A/, BC, B, 6-, %*, 4, D2;

    static 6b7ect @e1 = E;

    public static void main(String args[])

    {

    if( binar1Search(0, a.length"#) )

    S1stem.out.println(@e1 ! found in the list);

    else

    S1stem.out.println(@e1 ! not found in the list);

    static boolean binar1Search(int lo, int high)

    {

    if( lo & high ) return false;

    int mid = (lo ! high)'>;

    int c = (($omparable)@e1).compare%o(a[mid]);

    if( c 0) return binar1Search(lo, mid"#);

    else if( c & 0) return binar1Search(mid!#, high);

    else return true;

  • 8/11/2019 MTech ADSA Programs

    4/26

    . +rite aa programs to implement the List !-T using arrays and lin%ed lists.

    List ADTThe elements in a list are of generic type 6b7ect. The elements form a linear structure in which list elements follow

    one after the other, from the beginning of the list to its end. The list !-T supports the following operations:

    createList(int n): reates (initially) a list with n nodes.Input: integer$ Output: /oneinsertFirst(obj): *nserts ob0ect obj at the beginning of a list.

    Input: Ob0ect$ Output: /oneinsertAfter(obj, obj p): *nserts ob0ect obj after the obj p in a list.

    Input: Ob0ect and position$ Output: /oneobj deleteFirst(): -eletes the ob0ect at the beginning of a list.

    Input: /one$ Output: -eleted ob0ect obj.

    obj deleteAfter(obj p): -eletes the ob0ect after the obj p in a list.

    Input: 1osition$ Output: -eleted ob0ect obj.

    boolean isEmpty(): 2eturns a boolean indicating if the list is empty.

    Input: /one$ Output: boolean (true orfalse).

    int size(): 2eturns the number of items in the list.

    Input: /one$ Output: integer.

    Type Ob0ect may be any type that can be stored in the list. The actual type of the ob0ect will be proided by the user.

    The !-T is translated into a aa interface in the following program.

    public interface List

    {

    public void createList(int n);

    public void insertFirst(6b7ect ob);

    public void insertAfterb7ect ob, 6b7ect pos);

    public 6b7ect deleteFirst();

    public 6b7ect deleteAfter(6b7ect pos);

    public boolean isEmpty();

    public int size();

    classArrayList implements List

    {

    classNode

    { 6b7ect data;

    int neFt;

    *ode(6b7ect ob, int i) 33 constructor{ data = ob;

    neFt = i;

  • 8/11/2019 MTech ADSA Programs

    5/26

    int B/ESGHI; 33 ma number of nodes in the list

    *ode list[]; 33 create list array

    int head, count; 33 count: current number of nodes in the list

    /rra15ist( int s) 33 constructor{ B/ESGHI = s;

    list = ne *ode[B/ESGHI];

    public void initializeList()

    { for( int p = 0; p B/ESGHI"#; p!! )

    list[p] = ne *ode(null, p!#);

    list[B/ESGHI"#] = ne *ode(null, "#);

    public void createList(int n) 33 create 4n5 nodes{ int p;

    for( p = 0; p n; p!! )

    {

    list[p] = ne *ode(##!##Jp, p!#);

    count!!;

    list[p"#].neFt = "#; 33 end of the list

    public void insertFirst(6b7ect item)

    {

    if( count == B/ESGHI )

    { S1stem.out.println(JJJ5ist is K455);

    return;

    int p = get*ode();

    if( p L= "# )

    {

    list[p].data = item;

    if( isImpt1() ) list[p].neFt = "#;

    else list[p].neFt = head;

    head = p;

    count!!;

    public void insertAfter(6b7ect item, 6b7ect F)

    {

    if( count == B/ESGHI )

    { S1stem.out.println(JJJ5ist is K455);

    return;

    int M = get*ode(); 33 get the aailable position to insert new node

    int p = find(F); 33 get the inde (position) of the Ob0ect Fif( M L= "# )

    { list[M].data = item;

    list[M].neFt = list[p].neFt;

    list[p].neFt = M;count!!;

    public int getNode() 33 returns aailable node inde{ for( int p = 0; p B/ESGHI; p!! )

    if(list[p].data == null) return p;

    return "#;

    public int find(6b7ect ob) 33 find the inde (position) of the Ob0ect ob{ int p = head;

    hile( p L= "#)

    { if( list[p].data == ob ) return p;

  • 8/11/2019 MTech ADSA Programs

    6/26

    p = list[p].neFt; 33 adance to net node

    return "#;

    public 6b7ect deleteFirst()

    { if( isImpt1() ){ S1stem.out.println(5ist is empt13 no deletion);

    return null;

    6b7ect tmp = list[head].data;

    if( list[head].neFt == "# ) 33 if the list contains one node,

    head = "#; 33 ma%e list empty.else

    head = list[head].neFt;

    count""; 33 update countreturn tmp;

    public 6b7ect deleteAfter(6b7ect F)

    { int p = find(F);

    if( p == "# NN list[p].neFt == "# )

    { S1stem.out.println(*o deletion);

    return null;

    int M = list[p].neFt;

    6b7ect tmp = list[M].data;

    list[p].neFt = list[M].neFt;

    count"";

    return tmp;

    public void display()

    { int p = head;

    S1stem.out.print(On5ist3 [ );

    hile( p L= "#)

    { S1stem.out.print(list[p].data ! ); 33 print data

    p = list[p].neFt; 33 adance to net node

    S1stem.out.println(]On);''

    public boolean isEmpty()

    { if(count == 0) return true;

    else return false;

    public int size()

    { return count;

    class /rra15ist+emo

    {

    public static void main(String[] args)

    {

    /rra15ist lin@ed5ist = ne /rra15ist(#0);

    [email protected]();

    [email protected](:); 33 create 6 [email protected](); 33 print the listS1stem.out.print(GnsertKirst 3);

    [email protected]();

    [email protected]();

    S1stem.out.print(Gnsert ?? after 3);

    [email protected]/fter(??, ); 33 insert 77 after [email protected]();

    6b7ect item = [email protected](); S1stem.out.println(+eleted node3 ! item);

    [email protected]();

    S1stem.out.print(GnsertKirst

  • 8/11/2019 MTech ADSA Programs

    7/26

    [email protected](); 33 delete node after node 99S1stem.out.println(+eleted node3 ! item);

    [email protected]();

    S1stem.out.println(siPe()3 ! [email protected]());

    The following output is generated by this program:5ist3 [ ## >> :: ]

    GnsertKirst 3

    5ist3 [ ## >> :: ]

    Gnsert ?? after 3

    5ist3 [ ## >> ?? :: ]

    +eleted node3

    5ist3 [ ## >> ?? :: ]

    GnsertKirst ?? :: ]siPe()3

    Linked Implementation of ListLinkedList class implemented by List interface is gien 1rogram 7(d) and it is tested in 1rogram

    class LinkedList implements List

    {

    classNode

    { 6b7ect data; 33 data item

    *ode neFt; 33 refers to net node in the list

    *ode( 6b7ect d ) 33 constructor

    { data = d; 33 4neFtQ is automatically set to null

    *ode head; 33 head refers to first node

    *ode p; 33 p refers to current node

    int count; 33 current number of nodes

    public void createList(int n) 33 create ;n; nodes{

    p = ne *ode(##); 33 create first node

    head = p; 33 assign mem. address of ;p; to ;head;

    for( int i = #; i n; i!! ) 33 create ;n"#; nodesp = p.neFt = ne *ode(## ! ##Ji);

    count = n;

    public void insertFirst(6b7ect item) 33 insert at the beginning of list{

    p = ne *ode(item); 33 create new node

    p.neFt = head; 33 new node refers to old head

    head = p; 33 new head refers to new nodecount!!;

    public void insertAfter(6b7ect item,6b7ect @e1)

    {

    p = find(@e1); 33 get

  • 8/11/2019 MTech ADSA Programs

    8/26

    S1stem.out.println(@e1 ! @e1 is not found);

    else

    { *ode M = ne *ode(item); 33 create new node

    M.neFt = p.neFt; 33 new node net refers to p.net

    p.neFt = M; 33 p.net refers to new nodecount!!;

    public *ode find(6b7ect @e1)

    {

    p = head;

    hile( p L= null ) 33 start at beginning of list until end of list{

    if( p.data == @e1 ) return p; 33 if found, return %ey

    p = p.neFt; 33 moe to net node

    return null; 33 if %ey search is unsuccessful, return null

    public 6b7ect deleteFirst() 33 delete first node

    {if( isImpt1() )

    { S1stem.out.println(5ist is empt13 no deletion);

    return null;

    *ode tmp = head; 33 tmp saes reference to headhead = tmp.neFt;

    count"";

    return tmp.data;

    public 6b7ect deleteAfter(6b7ect @e1) 33 delete node after @e1 item

    { p = find(@e1); 33 p &

  • 8/11/2019 MTech ADSA Programs

    9/26

  • 8/11/2019 MTech ADSA Programs

    10/26

    Program: A Stack Inter!acepublic interfaceStack

    { public voidpush(6b7ect ob);

    public 6b7ectpop();

    public 6b7ectpeek();

    public boolean isEmpty();public int size();

    Thepush,pop,peek, empty, andsize operations are translated directly into specifications for methods named push(),

    pop(), pee@(), isImpt1(), and siPe() respectiely. These are conentional names for stac% operations. Bach

    method is defined by specifying its return alue and any changes that it ma%es to the ob0ect.

    Stack Im"#ementationThere are seeral ways to implement the Stac@ interface. The simplest is to use an ordinary array. This is done in

    1rogram C(b). The /rra1Stac@ implementation uses an array a[] to store elements of the stac%. *ts other datafield is the integer top, which refers top element of the stac%. The top is also used to count the current number of

    items in the stac%.

    Program (b): AnArrayStack $#ass

    public classArrayStack implements Stack

    {

    private 6b7ect a[];

    private int top; 33 stac% top

    public /rra1Stac@(int n) 33 constructor

    { a = ne 6b7ect[n]; 33 create stac% array

    top = "#; 33 no items in the stac%

    public voidpush(6b7ect item) 33 add an item on top of stac%{if(top == a.length"#)

    { S1stem.out.println(Stac@ is full);

    return;

    top!!; 33 increment top

    a[top] = item; 33 insert an item

    public 6b7ectpop() 33 remoe an item from top of stac%{

    if( isImpt1() )

    { S1stem.out.println(Stac@ is empt1);

    return null;

    6b7ect item = a[top]; 33 access top item

    top""; 33 decrement topreturn item;

    public 6b7ectpeek() 33 get top item of stac%{ if( isImpt1() ) return null;

    return a[top];

    public boolean isEmpty() 33 true if stac% is empty{ return (top == "#);

    public int size() 33 returns number of items in the stac%{ return top!#;

  • 8/11/2019 MTech ADSA Programs

    11/26

    The constructor creates a new stac% of a si@e, n specified in its argument. The ariable top stores the inde of the

    item on the top of the stac%.

    The push() method increments top so it points to the space 0ust aboe the preious top, and stores a data itemthere. /otice that top is incremented before the item is inserted. The pop() method returns the alue at top and

    then decrements top. This effectiely remoes the item from the stac%$ it is inaccessible, although the alue remains

    in the array (until another item is pushed into the cell). The pee@() method simply returns the alue at top, without

    changing the stac%. The specifications for the pop() and pee@() methods in the Stac@ interface require that the

    stac% be not empty. The isImpt1() method returns true if the stac% is empty. The top ariable is at # if the stac%

    is empty.

    The /rra1Stac@ class is tested in the 1rogram

    Program : TestingArrayStack $#ass

    class /rra1Stac@+emo

    {

    public static voidmain(String[] args){

    /rra1Stac@ st@ = ne /rra1Stac@(:); 33 create stac% of si@e 66b7ect item;

    [email protected](R/R); 33 push 8 items onto stac%[email protected](R2R);

    [email protected](R$R);

    S1stem.out.println(siPe()3 ! [email protected]());

    item = [email protected](); 33 delete itemS1stem.out.println(item ! is deleted);

    [email protected](R+R); 33 add three more items to the stac%[email protected](RIR);

    [email protected](RKR);

    S1stem.out.println([email protected]() ! is deleted);

    [email protected](RTR); 33 push one itemitem = [email protected]@(); 33 get top item from the stac%S1stem.out.println(item ! is on top of stac@);

    Output of this program is:siPe()3

    $ is deleted

    Stac@ is full

    I is deleted

    T is on top of stac@

    ueue ADT

    The elements in a queue are of generic type Ob0ect. The queue elements are linearly ordered from the front to the

    rear. Blements are inserted at the rear of the queue (enqueued) and are remoed from the front of the queue(dequeued). ! Dueue is an !bstract -ata Type (!-T) that supports the following methods:insert(obj): !dds ob0ect obj at the rear of a queue.

    Input: Ob0ect$ Output: /one.

    obj remove(): -eletes an item from the front of a queue and returns ob0ect obj$ an error occurs if the queue is empty.

    Input: /one$ Output: Ob0ect.

    objpeek(): 2eturns the ob0ect obj at the front of a queue , without remoing it$ an error occurs if the queue is empty.

    Input: /one$ Output: Ob0ect.

    boolean isEmpty(): 2eturns a boolean indicating if the queue is empty.

  • 8/11/2019 MTech ADSA Programs

    12/26

    Input: /one$ Output: boolean (true orfalse).

    int size(): 2eturns the number of items in the queue.

    Input: /one$ Output: integer.

    Type Ob0ect may be any type that can be stored in the queue. The actual type of the ob0ect will be proided by the

    user. The !-T is translated into a aa interface in 1rogram

    Program : A ueue Inter!acepublic interface ueue

    {

    public void insert(6b7ect ob);

    public 6b7ect remove();

    public 6b7ectpeek();

    public boolean isEmpty();

    public int size();

    /ote the similarities between these specifications and that of the stac% interface. The only real difference, between the

    names of the operations, is that the queue adds new elements at the opposite end from which they are accessed, while

    the stac% adds them at the same end.

    ueue Im"#ementation

    The /rra1Uueue implementation of Mueue interface is done by ta%ing an array, que'n and treating it as if it were

    circular. The elements are inserted by increasing rear to the net free position. +hen rear & nE, the net element is

    entered at que'# in case that spot is free. That is, the element que'nE follows que'#. 1rogram C(e) implements the/rra1Uueue class, and 1rogram C(f) tests this class.

    Program : AnArrayueue $#assclassArrayueue implements ueue

    { private int maFSiPe; 33 maimum queue si@e

    private 6b7ect[] Mue; 33 que is an arrayprivate int front;

    private int rear;

    private int count; 33 count of items in queue (queue si@e)

    public /rra1Uueue(int s) 33 constructor{ maFSiPe = s;

    Mue = ne 6b7ect[maFSiPe];

    front = rear = "#;

    count = 0;

    public void insert(6b7ect item) 33 add item at rear of queue{

    if( count == maFSiPe )

    { S1stem.out.println(Uueue is Kull); return;

    if(rear == maFSiPe"# NN rear == "#)

    { Mue[0] = item;

    rear = 0;

    if( front == "#) front = 0;

    else Mue[!!rear] = item;

    count!!; 33 update queue si@e

    public 6b7ect remove() 33 delete item from front of queue{

    if( isImpt1() )

    {S1stem.out.println(Uueue is Impt1); return 0;

    6b7ect tmp = Mue[front]; 33 sae item to be deleted

  • 8/11/2019 MTech ADSA Programs

    13/26

    Mue[front] = null; 33 ma%e deleted item5s cell emptyif( front == rear )

    rear = front = "#;

    else if( front == maFSiPe"# ) front = 0;

    else front!!;

    count""; 33 less one item from the queue si@ereturn tmp;

    public 6b7ectpeek() 33 pee% at front of the queue{ return Mue[front];

    public boolean isEmpty() 33 true if the queue is empty{ return (count == 0);

    public int size() 33 current number of items in the queue{ return count;

    public void displayAll()

    {

    S1stem.out.print(Uueue3 );

    for( int i = 0; i maFSiPe; i!! )

    S1stem.out.print( Mue[i] ! );

    S1stem.out.println();

    Program %&(!): TestingArrayueue c#assclass ueueDemo

    {

    public static voidmain(String[] args)

    {

    3F queue holds a ma of > items F3/rra1Uueue M = ne /rra1Uueue();

    6b7ect item;

    M.insert(R/R); M.insert(R2R); M.insert(R$R); M.displa1/ll();

    item = M.remove(); 33 delete itemS1stem.out.println(item ! is deleted);

    item = M.remove();S1stem.out.println(item ! is deleted);

    M.displa1/ll();

    M.insert(R+R); 33 insert 8 more itemsM.insert(RIR);

    M.insert(RKR);

    M.displa1/ll();

    item = M.remove();

    S1stem.out.println(item ! is deleted);

    M.displa1/ll();

    S1stem.out.println(pee@()3 ! M.pee@());

    M.insert(RTR);

    M.displa1/ll();

    S1stem.out.println(Uueue siPe3 !

    M.siPe());

    Output of this program is as follows:

  • 8/11/2019 MTech ADSA Programs

    14/26

  • 8/11/2019 MTech ADSA Programs

    15/26

    static boolean isalindrome(String str)

    {

    5in@ed5ist$haracter& Mue = ne 5in@ed5ist$haracter&();

    int n = str.length();

    for( int i=0; i n; i!! )

    Mue.add5ast(str.char/t(i));for( int i=n"#; i & n'>; i"" )

    if( str.char/t(i) L= Mue.removeKirst() ) return false;

    return true;

    ?. Write Java programs to implement the folloing using a singly lin&ed list.

    (a) Stac& '(b) *ueue '

    Linked Implementation o! a "tackclassNode

    { int data; 33 data item*ode neFt; 33 net node in lin%edEstac%

    *ode( int d ) 33 constructor

    { data = d; 33 net is automatically set to null

    class LinkedStack

    {

    *ode top; 33 top refers to topEnode

    *ode p; 33 p refers to current node

    public voidpush(int item) 33 add item onto stac%{

    p = ne *ode(item); 33 create new node

    p.neFt = top; 33 new node refers to old top

    top = p; 33 top refers to new node

    public *odepop() 33 remoe a node from the stac%{

    if( isImpt1() )

    { S1stem.out.println(Stac@ is empt1);

    return null;

    *ode tmp = top; 33 tmp saes reference to top node

    top = tmp.neFt; 33 now, top refers to net node of old top

    return tmp; 33 return the popped item

    public *odepeek() 33 get top node from the stac%, without deleting{

    if( isImpt1() ){ S1stem.out.println(Stac@ is empt1);

    return null;

    return top;

    public void displayStack()

    {

    p = top; 33 p refers to top

    S1stem.out.print(On$ontents of Stac@3 [ );

    hile( p L= null ) 33 start printing from top of stac% to bottom of stac%{

  • 8/11/2019 MTech ADSA Programs

    16/26

    S1stem.out.print(p.data ! ); 33 print data

    p = p.neFt; 33 moe to net node

    S1stem.out.println(]);

    public boolean isEmpty() 33 true if stac% is empty{ return (top == null);

    ''''''''''''''''''''''''' LinkedStackDemo.java '''''''''''''''''

    class LinkedStackDemo

    {

    public static voidmain(String[] args)

    {

    5in@edStac@ st@ = ne 5in@edStac@(); 33 create stac% ob0ect

    *ode item; 33 item stores popped node

    [email protected](>0); 33 add 9#, 8>, 6# to stac%[email protected]();

    [email protected](:0);

    [email protected]@(); 33 print contents of stac%

    item = [email protected](); 33 remoe a node from the top and print itif( item L= null )

    {

    S1stem.out.println(opped item3 ! item.data);

    [email protected]@();

    [email protected](?); 33 insert 7>, C#, C>[email protected](0 ]

    $ontents of Stac@3 [

  • 8/11/2019 MTech ADSA Programs

    17/26

    *ode front, rear;

    int count;

    public void insert(6b7ect item)

    {

    *ode p = ne *ode(item);

    if(front == null) 33 queue is empty$ insert first item{ front = rear = p;

    rear.neFt = null;

    if(front == rear) 33 queue contains one item$ insert second item{ rear = p;

    front.neFt = rear;

    rear.neFt = null;

    else 33 queue contains 9 or more items{ rear.neFt = p; 33 old rear.neFt refers to p

    rear = p; 33 new rear refers to prear.neFt = null;

    count!!; 33 increment queue si@epublic 6b7ect remove()

    { if(isImpt1())

    { S1stem.out.println(U is empt1); return null;

    6b7ect item = front.data;

    front = front.neFt;

    count""; 33 decrement queue si@ereturn item;

    public boolean isEmpty()

    { return (front == null);

    public 6b7ectpeek()

    { return front.data;

    public int size()

    { return count; public void display()

    { *ode p = front;

    S1stem.out.print(5in@ed U3 );

    if(p == null) S1stem.out.println(empt1);

    hile( p L= null )

    {

    S1stem.out.print(p.data ! );

    p = p.neFt;

    S1stem.out.println();

    Testing Linkedueue $lass

    class LinkedueueDemo{ public static voidmain(String[] args)

    {

    5in@edUueue M = ne 5in@edUueue();

    M.displa1();

    M.insert(R/R);

    M.insert(R2R);

    M.insert(R$R);

    M.insert(R+R);

    M.displa1();

    S1stem.out.println(delete()3 ! M.remove());

    M.displa1();

    S1stem.out.println(pee@()3 ! M.pee@());

  • 8/11/2019 MTech ADSA Programs

    18/26

  • 8/11/2019 MTech ADSA Programs

    19/26

    }

    race of $ubble sort (elementsto be interchanged are shon

    in re' colour)pass % 2 * + , & Process

    E 9C &' 8C > C> 78 7> Original

    array

    9C 8> &' * > C> 78 7> 6H, 8>

    interc

    hange

    d

    9C 8> 8C &' + C> 78 7> 6H, 8C

    interchange

    d

    9C 8> 8C > 6H * , 7> 6H, >

    interc

    hange

    d

    9C 8> 8C > 6H 78 * , C>, 78interc

    hange

    d

    9C 8> * + 6H 78 7> C> C>, 7>

    interc

    hange

    d

    9 9C + 8C 6H 78 7> C> 8C, >interc

    hange

    d

    8 2* + 8> 8C 6H 78 7> C> 8>, >

  • 8/11/2019 MTech ADSA Programs

    20/26

    interchange

    d

    6 > 9C 8> 8C 6H 78 7> C> 9C, >

    interc

    hanged

    "election "ort

    race of Selection sort (elements to beinterchanged are shon in re' colour) % 2 * + , &

    pass min a-pass. a-min. swap

    a-pass./

    a-min.

    &' 9C 7> 8C + C> 78 7# # 6 6H > 6H, >

    > 9C 7> 8C 6H C> 78 7# 9C 9C min &pass

    no echange

    > 9C , * 6H C> 78 7# 9 8 7> 8C 7>, 8C

    > 9C 8C , &' C> 78 7# 8 6 7> 6H 7>, 6H

    > 9C 8C 6H , C> 78 ,0 6 C 7> 7# 7>, 7#

    > 9C 8C 6H 7# * , 7> > 7 C> 78 C>, 78

    > 9C 8C 6H 7# 78 * , 7 C C> 7> C>, 7>

    > 9C 8C 6H 7# 78 7> C> Aorted list

  • 8/11/2019 MTech ADSA Programs

    21/26

    "election "ort

    class SelectionSort+emo

    {

    public static void main(String[] args)

    {

    int[] arr = { :9, >

  • 8/11/2019 MTech ADSA Programs

    22/26

    if( a[i] a[min] ) min = i;

    if( min L= pass )

    {

    int tmp = a[min];

    a[min] = a[pass];

    a[pass] = tmp;

  • 8/11/2019 MTech ADSA Programs

    23/26

    static void displa1( int a[] )

    {

    for( int i = 0; i a.length; i!! )

    S1stem.out.print( a[i] ! );

    1ollowing output is generated !rom this program:

    4nsorted arra13 :9 >< ? < # 9C 8C 6H 7# 78 7> C>

    uick "ort

    class Uuic@Sort+emo

    {

    public static void main(String[] args)

    {

    int[] arr = { ?, , #, 90, , 8, ;

    S1stem.out.print(On 4nsorted arra13 );

    displa1( arr );

    Muic@Sort( arr, 0, arr.length"# );

    S1stem.out.print(On Sorted arra13 );

    displa1( arr );

  • 8/11/2019 MTech ADSA Programs

    24/26

    static void "uickSort(int a[], int left, int right)

    {

    int neleft = left, neright = right;

    int amid, tmp;

    amid = a[(left ! right)'>]; 33 piot is amid

    do 33 doEwhileEloop

    {

    hile( (a[neleft] amid) YY (neleft right))

    neleft!!;

    hile( (amid a[neright]) YY (neright & left))

    neright"";

    if(neleft = neright)

    { tmp = a[neleft];

    a[neleft] = a[neright];

    a[neright] = tmp;

    neleft!!; neright"";

    hile(neleft = neright); 33 end of doEwhileEloop

  • 8/11/2019 MTech ADSA Programs

    25/26

    if(left neright) Muic@Sort(a, left, neright);

    if(neleft right) Muic@Sort(a, neleft, right);

    static void displa1( int a[] )

    {

    for( int i = 0; i a.length; i!! )

    S1stem.out.print( a[i] ! );

    }

    3utput:

    4nsorted arra13 ? # 90 8

    Sorted arra13 # > :0 : ?0 ?

  • 8/11/2019 MTech ADSA Programs

    26/26

    6 15 25 35 4 45 C> H# 7# ' 7> > >> > '

    > 15 25 35 4 45 C> H# ,0 >> 7> > !5 > # ,0

    7 15 25 35 4 45 " H# C> 7> > !5 > 7

    C 15 25 35 4 45 55 " H# * 7> > !5 C # *

    15 25 35 4 45 55 " "5 #5 '0 > !5 H # '0

    H 15 25 35 4 45 55 " "5 #5 $5 ! !5 sorted array