Tuesday 28 January 2014

Implement bubble sort in java

Bubble sort, also referred to as sinking sort, is a simple sorting algorithm that works by repeatedly 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 are 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. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.


Bubble Sort


Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.Performance of bubble sort over an already-sorted list (best-case) is O(n).



public class MyBubbleSort

// logic to sort the elements
public static void bubble_srt(int array[])
int n = array.length;
int k;
for (int m = n; m >= 0; m--)
for (int i = 0; i < n - 1; i++)
k = i + 1;
if (array[i] > array[k])
swapNumbers(i, k, array);


printNumbers(array);



private static void swapNumbers(int i, int j, int[] array)

int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;


private static void printNumbers(int[] input)

for (int i = 0; i < input.length; i++)
System.out.print(input[i] + ", ");

System.out.println("\n");


public static void main(String[] args)
int[] input = 4, 2, 9, 6, 23, 12, 34, 0, 1 ;
bubble_srt(input);




 


Output


2, 4, 6, 9, 12, 23, 0, 1, 34
2, 4, 6, 9, 12, 0, 1, 23, 34
2, 4, 6, 9, 0, 1, 12, 23, 34
2, 4, 6, 0, 1, 9, 12, 23, 34
2, 4, 0, 1, 6, 9, 12, 23, 34
2, 0, 1, 4, 6, 9, 12, 23, 34
0, 1, 2, 4, 6, 9, 12, 23, 34
0, 1, 2, 4, 6, 9, 12, 23, 34
0, 1, 2, 4, 6, 9, 12, 23, 34
0, 1, 2, 4, 6, 9, 12, 23, 34


Implement bubble sort in java

put in force bubble type in java

Bubble kind, additionally known as sinking sort, is a simple sorting algorithm that works by means of time and again stepping in the course of the list to be sorted, evaluating each and every pair of adjacent gadgets and swapping them if they are within the mistaken order. The cross in the course of the listing is repeated except no swaps are needed, which indicates that the record is sorted. The algorithm gets its title from the way in which smaller elements “bubble” to the highest of the checklist. as a result of it best makes use of comparisons to operate on components, it is a comparison type. despite the fact that the algorithm is unassuming, most of the different sorting algorithms are extra efficient for giant lists.


Implement bubble sort in java


Bubble type has worst-case and reasonable complexity each О(n2), the place n is the number of items being sorted. There exist many sorting algorithms with substantially higher worst-case or moderate complexity of O(n log n). Even other О(n2) sorting algorithms, equivalent to insertion type, are inclined to have higher performance than bubble kind. therefore, bubble type is just not a practical sorting algorithm when n is huge.efficiency of bubble kind over an already-sorted list (perfect-case) is O(n).


public class MyBubbleSort 

// logic to kind the weather
public static void bubble_srt(int array[])
int n = array.length;
int k;
for (int m = n; m >= zero; m--)
for (int i = 0; i < n - 1; i++)
ok = i + 1;
if (array[i] > array[k])
swapNumbers(i, k, array);


printNumbers(array);



personal static void swapNumbers(int i, int j, int[] array)

int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;


non-public static void printNumbers(int[] enter)

for (int i = zero; i < input.size; i++)
gadget.out.print(input[i] + ", ");

system.out.println("n");


public static void main(String[] args)
int[] input = 4, 2, 9, 6, 23, 12, 34, zero, 1 ;
bubble_srt(input);



Output


2, four, 6, 9, 12, 23, 0, 1, 34
2, 4, 6, 9, 12, zero, 1, 23, 34
2, 4, 6, 9, zero, 1, 12, 23, 34
2, four, 6, zero, 1, 9, 12, 23, 34
2, 4, zero, 1, 6, 9, 12, 23, 34
2, 0, 1, 4, 6, 9, 12, 23, 34
zero, 1, 2, four, 6, 9, 12, 23, 34
zero, 1, 2, four, 6, 9, 12, 23, 34
zero, 1, 2, 4, 6, 9, 12, 23, 34
zero, 1, 2, 4, 6, 9, 12, 23, 34


put in force bubble type in java

Implement bubble sort in java

Bubble sort, also referred to as sinking sort, is a simple sorting algorithm that works by repeatedly 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 are 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. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.


Implement bubble sort in java


Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.Performance of bubble sort over an already-sorted list (best-case) is O(n).


 


package com.java2novice.algos;

public class MyBubbleSort

// logic to sort the elements
public static void bubble_srt(int array[])
int n = array.length;
int k;
for (int m = n; m >= 0; m--)
for (int i = 0; i array[k])
swapNumbers(i, k, array);


printNumbers(array);



private static void swapNumbers(int i, int j, int[] array)

int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;


private static void printNumbers(int[] input)

for (int i = 0; i

Output



2, 4, 6, 9, 12, 23, 0, 1, 34,

2, 4, 6, 9, 12, 0, 1, 23, 34,

2, 4, 6, 9, 0, 1, 12, 23, 34,

2, 4, 6, 0, 1, 9, 12, 23, 34,

2, 4, 0, 1, 6, 9, 12, 23, 34,

2, 0, 1, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,

0, 1, 2, 4, 6, 9, 12, 23, 34,


Implement bubble sort in java

Wednesday 18 December 2013

C Program to Find the First Occurence of the any Character of String2 in String1

#include < stdio.h>
 
void main()
{
    char s1[50], s2[10];
    int i, flag = 0;
    char *ptr1, *ptr2;
 
    printf("\nenter the string1:" );
    scanf(" %[^\n]s" , s1);    
    printf("\nenter the string2:" );
    scanf(" %[^\n]s" , s2);
 
    /*COMPARING THE STRING1 CHARACTER BY CHARACTER WITH ALL CHARACTERS OF STRING1*/
    for (i = 0, ptr1 = s1;*ptr1 !=  '\0';ptr1++)
    {
        i++;
        for (ptr2 = s2; *ptr2 != '\0';ptr2++)
        {
            if (*ptr1  ==  *ptr2)
            {
                flag = 1;
                break;
            }
        }
        if (flag  ==  1)
            break;
    }
 
    if (flag  ==  1)
        printf("\nfirst occurance of character of string2 in string1 is at position:%d and character is %c" , i, *ptr2);
    else
        printf("\nnone of the characters of string1 match with mone of characters of string2" );
}

Output

  
enter the string1:C Programming Class
 
enter the string2:rnp
 
first occurance of character of string2  in  string1 is at position: 3  and character is p

For More Details Please Visit Ictjobs.info

C Program to Insert Character/Word in any Desired Location in a String

#include < stdio.h>
#include < string.h>
 
void main()
{
    int i, j, count = 0, pos, flag = 0;
    char s1[100], s2[10], s3[100];
    char *ptr1, *ptr2, *ptr3;
 
    printf("\nenter the String:" );
    scanf(" %[^\n]s" , s1);
    printf("\nenter the string to be inserted:" );
    scanf(" %[^\n]s" , s2);
    printf("\nenter the position you like to insert:" );
    scanf("%d" , &pos);
 
    ptr1 = s1;
    ptr3 = s3;
    /*COPYING THE GIVEN STRING TO NEW ARRAY AND INSERTING THE STRING IN NEW ARRAY*/
    for (i = 0, j = 0;*ptr1 != '\0'; ptr1++, i++, j++, ptr3++)
    {
        s3[j] = s1[i];
        if (*ptr1 == ' ' && flag != 1)
            ++count;
        if (flag != 1 && count == pos - 1)
        {
            flag = 1;
            for(ptr2 = s2;*ptr2 != '\0'; ptr2++)
            {
                s3[++j] = *ptr2;
                ptr3++;
            }
            s3[++j] = ' ';
            ptr3++;
        }
    }
    s3[j] = '\0';
    printf("\nthe string after modification is\n\n %s\n" , s3);
}

Output

 enter the string:Welcome to Sanfoundry's C Programming Class,  Welcome Again to C Class!
enter the word to insert:Sanfoundry' s
enter the position you like to insert: 3 
the string after modification is
 
Welcome to Sanfounsry's Sanfoundry' s C Programming Class,  Welcome Again to C Class ! 

For More Details Please Visit Ictjobs.info

C Program to Count the Total Number of Words in the Sentence using Command Line Argument

#include < stdio.h>
 
int main(int argc, char *argv[])
{
    int i = 0;
 
    /* If no sentence is given in the command line */
    if (argc == 1)
    {
        printf("\n No sentence given on command line" );
        return;
    }
    else
    {
        printf("\nThe words in the sentence are:" );
        /*
         * From argv[1] to argv[argc -1] calculate the number of arguments
         */
        for (i = 1;i < argc;i++)
        {
            printf("\n%s" , argv[i]);
         }
        printf("\n\nTotal number of words:" );
        printf(" %d" , argc-1);
    }
}

Output

 Welcome to C Class
 
The words  in  the sentence are:
Welcome
to
C
Class
 
Total number of words:  4 
 
 
 
No sentence given on  command  line

For More Details Please Visit Ictjobs.info

C Program to Replace all the Characters by Lowercase

#include < stdio.h>
#include < stdlib.h>
 
void main(int argc, char* argv[])
{
    FILE *fp1;
    int ch;
 
    if ((fp1 = fopen(argv[1], "r+" )) == NULL)
    {
        printf("\nfile cant be opened" );
        exit(0);
    }
    ch = fgetc(fp1);
    while (ch != EOF)
    {
        if (ch >= 65 && ch <= 90)
        {
            fseek(fp1, -1L, 1);
            fputc(ch + 32, fp1);
        }
        ch = fgetc(fp1);
    }
}

Output

 file4test
   cat  file4test
chandana chanikya
ravella

For More Details Please Visit Ictjobs.info