“Tipo de bolha” Respostas de código

Tipo de bolha

// C program for implementation of Bubble sort
#include <stdio.h>
  
void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
  
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
   int i, j;
   for (i = 0; i < n-1; i++)      
  
       // Last i elements are already in place   
       for (j = 0; j < n-i-1; j++) 
           if (arr[j] > arr[j+1])
              swap(&arr[j], &arr[j+1]);
}
  
/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
  
// Driver program to test above functions
int main()
{
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;
}
Healthy Hare

Tipo de bolha

public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
    }  
    public static void main(String[] args) {  
                int arr[] ={3,60,35,2,45,320,5};  
                 
                System.out.println("Array Before Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
                System.out.println();  
                  
                bubbleSort(arr);//sorting array elements using bubble sort  
                 
                System.out.println("Array After Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
   
        }  
}  
Ill Ibis

Tipo de bolha

import java.util.Arrays;
public class Bubble{
	public static void main(String[] args){
		int[] arr = {5,4,3,2,1}; // Test case array
		
		for(int i =0;i<arr.length-1;i++){ // Outer Loop
		boolean swap = false;
		  for(int j =1;j<arr.length;j++){ // Inner Loop
			  if(arr[j-1]>arr[j]){ // Swapping
				  int temp = arr[j-1];
				  arr[j-1] = arr[j];
				  arr[j] = temp;
				  swap = true;
			  }
		  }
		  if(!swap){ // If you went through the whole arrray ones and 
             break; // the elements did not swap it means the array is sorted hence stop  
		  }
		}
		
		System.out.print(Arrays.toString(arr));
	}
}
Sushant Mishra

Tipo de bolha

public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
    }  
    public static void main(String[] args) {  
                int arr[] ={3,60,35,2,45,320,5};  
                 
                System.out.println("Array Before Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
                System.out.println();  
                  
                bubbleSort(arr);//sorting array elements using bubble sort  
                 
                System.out.println("Array After Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
   
        }  
}  
Ill Ibis

Tipo de bolha

#include<iostream>
void swap(int &a, int &b)
{
  int temp=a;
  a=b;
  b=temp;
}
void bubblesort(int arr[], int n)
{
  for(int i=0;i<n-1;i++)// why upto n-1?
  {
    for(int j=0;j<n-1;j++)
    {
      if(arr[j]>arr[j+1])
      {
        swap(arr[j],arr[j+1]);
      }
    }
  }
}
int main()
{
  int arr[5]={7, 8, 1, -4, 0};
  bubblesort(arr, 5);
  for(int i=0;i<5;i++)
  {
    std::cout<<arr[i]<<std::endl;
  }
}
Jitu Biswas

Tipo de bolha

#include<bits/stdc++.h>
#define swap(x,y) { x = x + y; y = x - y; x = x - y; }

using namespace std;

/**
    * Function to Sort the array using Modified Bubble Sort Algorithm
    * @param arr: Array to be Sorted
    * @param n: Size of array
    * @return : None
*/
void bubbleSort(int arr[], int n)
{
    int i, j;
    bool flag;
    // Outer pass
    for(i = 0; i < n; i++)
    {
        flag = false;                                   // Set flag as false
        for(j = 0; j < n-i-1; j++)
        {
            // Compare values
            if( arr[j] > arr[j+1])
            {
                swap(arr[j],arr[j+1]);
                flag = true;
            }
        }
        // If no to elements are swapped then
        // array is sorted. Hence Break the loop.
        if(!flag)
        {
            break;
        }
    }
}
int main(int argv, char* argc[])
{
    int arr[] = {1,5,6,8,3,4,7,2,9};
    int n = sizeof(arr)/sizeof(int);
    cout<<"Unsorted Array :";
    for(int i=0;i<n;i++)                            // Print the Original Array
        cout<<arr[i]<<" ";
    cout<<endl;
    bubbleSort(arr,n);                              // Call for Bubble Sort Function
    cout<<"Sorted Array :";
    for(int i=0;i<n;i++)                            // Print the Sorted Array
        cout<<arr[i]<<" ";
    return(0);
}
Itchy Impala

Tipo de bolha

int *BubbleSort(int ar[], int size)
{
    bool sorted = false;
    while (sorted == false)
    {
        sorted = true;
        for (int i = 0; i < size; i++)
        {
            if (ar[i] > ar[i + 1])
            {
                swap(ar[i], ar[i + 1]);
                sorted = false;
            }
        }
    }
    return ar;
}
Clever Curlew

Respostas semelhantes a “Tipo de bolha”

Perguntas semelhantes a “Tipo de bolha”

Mais respostas relacionadas para “Tipo de bolha” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código