Algorithm BubbleSort is a simple sorting algorithm, it works by repeatly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps or needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
For Example :
import java.util.*;
class node1 {
int data;
node1 next, prev;
public node1(int n) {
data = n;
next = prev = null;
}
}
//Class list
class list {
node1 head, tail;
public list() {
head = tail = null;
}
void addLast(int n) {
node1 input = new node1(n);
if (head == null) {
head = tail = input;
} else {
tail.next = input;
input.prev = tail;
tail = input;
}
}
// for sorting
void sorting() {
node1 once = head, second = head, vloop = head;
while (vloop != tail) {
once = head;
while (once != null) {
second = once.next;
if (second != null) {
if (second.data < once.data)
vchange(once, second);
} once = once.next;
} vloop = vloop.next;
}
}
void vchange(node1 once, node1 second) {
int temp = second.data;
second.data = once.data;
once.data = temp;
}
void vprint() {
node1 temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}
//main class
public class BubleSort {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Total by sorting : ");
int n = input.nextInt();
list vnumber = new list();
System.out.println("Input number :");
for (int i = 0; i < n; i++) {
System.out.print(">> ");
int a = input.nextInt();
v_number.addLast(a);
}
System.out.println("Sort by Ascending :");
for (int i = 0; i < n - 1; i++) {
vnumber.sorting();
}
vnumber.vprint();
System.out.println();
}
}
gak ngerti sob...hehehe
ReplyDelete@Alrezamittariq,
ReplyDeletedipahami dulu aja urutan codingnya,.. itu coding permintaan dari sahabat yang ada di Yaman
@neta,.
makasih ya,. maaf telat
nice info sob, success for all
ReplyDeleteto Frank,...
ReplyDeletethanks your comment,... ill come in your blog
to digest,..
thanks