java实现六大排序算法

本文详细介绍了Java中冒泡排序、选择排序、希尔排序、插入排序、堆排序和合并排序的基本实现,以及它们在数组排序中的应用和运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、冒泡排序算法

package com.xxx.order;

public class maopao {
        public static void main(String[] args) {
            int[] arr = {64, 34, 25, 12, 22, 11, 90};
            bubbleSort(arr);
            System.out.println("Sorted array: ");
            printArray(arr);
        }

        static void bubbleSort(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n - i - 1; j++) {
                    if (arr[j] > arr[j + 1]) {
                        // Swap arr[j+1] and arr[j]
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
        }

        /* Prints the array */
        static void printArray(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n; ++i)
                System.out.print(arr[i] + " ");
            System.out.println();
        }

}

运行结果:

Sorted array: 
11 12 22 25 34 64 90 

二、选择排序算法

public class SelectionSortExample {
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        selectionSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void selectionSort(int[] arr) {  
        int n = arr.length;  
        for (int i = 0; i < n-1; i++) {  
            int minIndex = i;  
            for (int j = i+1; j < n; j++) {  
                if (arr[j] < arr[minIndex]) {  
                    minIndex = j;  
                }  
            }  
            int temp = arr[minIndex];  
            arr[minIndex] = arr[i];  
            arr[i] = temp;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

Sorted array: 
11 12 22 25 64 

三、希尔排序算法

public class ShellSortExample {
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        shellSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void shellSort(int[] arr) {  
        int n = arr.length;  
        int gap = n/2;  
        while (gap > 0) {  
            for (int i = gap; i < n; i++) {  
                int temp = arr[i];  
                int j;  
                for (j = i; j >= gap && arr[j-gap] > temp; j -= gap) {  
                    arr[j] = arr[j-gap];  
                }  
                arr[j] = temp;  
            }  
            gap /= 2;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

Sorted array: 
11 12 22 25 64 

四、插入排序算法

public class InsertionSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        insertionSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void insertionSort(int[] arr) {  
        int n = arr.length;  
        for (int i = 1; i < n; i++) {  
            int key = arr[i];  
            int j = i - 1;  
            while (j >= 0 && arr[j] > key) {  
                arr[j + 1] = arr[j];  
                j = j - 1;  
            }  
            arr[j + 1] = key;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

Sorted array: 
11 12 22 25 64 

五、堆排序算法

public class HeapSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        heapSort(arr);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void heapSort(int[] arr) {  
        int n = arr.length;  
        // Build heap  
        for (int i = n / 2 - 1; i >= 0; i--) {  
            heapify(arr, n, i);  
        }  
        // One by one extract an element from heap  
        for (int i = n - 1; i >= 0; i--) {  
            // Move current root to end  
            int temp = arr[0];  
            arr[0] = arr[i];  
            arr[i] = temp;  
            // call max heapify on the reduced heap  
            heapify(arr, i, 0);  
        }  
    }  
  
    /* To heapify a subtree rooted with node i which is an index in arr[]. n is size of heap */  
    static void heapify(int[] arr, int n, int i) {  
        int largest = i; // Initialize largest as root  
        int left = 2 * i + 1; // left = 2*i + 1  
        int right = 2 * i + 2; // right = 2*i + 2  
        // If left child is larger than root  
        if (left < n && arr[left] > arr[largest]) {  
            largest = left;  
        }  
        // If right child is larger than largest so far  
        if (right < n && arr[right] > arr[largest]) {  
            largest = right;  
        }  
        // If largest is not root  
        if (largest != i) {  
            int swap = arr[i];  
            arr[i] = arr[largest];  
            arr[largest] = swap;  
            // Recursively heapify the affected sub-tree  
            heapify(arr, n, largest);  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

Sorted array: 
11 12 22 25 64 

六、合并排序算法

public class MergeSortExample {  
    public static void main(String[] args) {  
        int[] arr = {64, 25, 12, 22, 11};  
        mergeSort(arr, 0, arr.length - 1);  
        System.out.println("Sorted array: ");  
        printArray(arr);  
    }  
  
    static void mergeSort(int[] arr, int l, int r) {  
        if (l < r) {  
            int m = (l + r) / 2;  
            mergeSort(arr, l, m);  
            mergeSort(arr, m + 1, r);  
            merge(arr, l, m, r);  
        }  
    }  
  
    static void merge(int[] arr, int l, int m, int r) {  
        int n1 = m - l + 1;  
        int n2 = r - m;  
        int L[] = new int[n1];  
        int R[] = new int[n2];  
        for (int i = 0; i < n1; ++i) {  
            L[i] = arr[l + i];  
        }  
        for (int j = 0; j < n2; ++j) {  
            R[j] = arr[m + 1 + j];  
        }  
        int i = 0, j = 0;  
        int k = l;  
        while (i < n1 && j < n2) {  
            if (L[i] <= R[j]) {  
                arr[k] = L[i];  
                i++;  
            } else {  
                arr[k] = R[j];  
                j++;  
            }  
            k++;  
        }  
        while (i < n1) {  
            arr[k] = L[i];  
            i++;  
            k++;  
        }  
        while (j < n2) {  
            arr[k] = R[j];  
            j++;  
            k++;  
        }  
    }  
  
    /* Prints the array */  
    static void printArray(int[] arr) {  
        int n = arr.length;  
        for (int i=0; i<n; ++i) {  
            System.out.print(arr[i]+" ");  
        }  
        System.out.println();  
    }  
}

运行结果:

Sorted array: 
11 12 22 25 64
### LlamaIndex 多模态 RAG 实现 LlamaIndex 支持多种数据类型的接入与处理,这使得它成为构建多模态检索增强生成(RAG)系统的理想选择[^1]。为了实现这一目标,LlamaIndex 结合了不同种类的数据连接器、索引机制以及强大的查询引擎。 #### 数据连接器支持多样化输入源 对于多模态数据的支持始于数据收集阶段。LlamaIndex 的数据连接器可以从多个异构资源中提取信息,包括但不限于APIs、PDF文档、SQL数据库等。这意味着无论是文本还是多媒体文件中的内容都可以被纳入到后续的分析流程之中。 #### 统一化的中间表示形式 一旦获取到了原始资料之后,下一步就是创建统一而高效的内部表达方式——即所谓的“中间表示”。这种转换不仅简化了下游任务的操作难度,同时也提高了整个系统的性能表现。尤其当面对复杂场景下的混合型数据集时,良好的设计尤为关键。 #### 查询引擎助力跨媒体理解能力 借助于内置的强大搜索引擎组件,用户可以通过自然语言提问的形式轻松获得所需答案;而对于更复杂的交互需求,则提供了专门定制版聊天机器人服务作为补充选项之一。更重要的是,在这里实现了真正的语义级关联匹配逻辑,从而让计算机具备了一定程度上的‘认知’功能去理解和回应人类意图背后所蕴含的意义所在。 #### 应用实例展示 考虑到实际应用场景的需求多样性,下面给出一段Python代码示例来说明如何利用LlamaIndex搭建一个多模态RAG系统: ```python from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, PromptHelper, ServiceContext from langchain.llms.base import BaseLLM import os def create_multi_modal_rag_system(): documents = SimpleDirectoryReader(input_dir='./data').load_data() llm_predictor = LLMPredictor(llm=BaseLLM()) # 假设已经定义好了具体的大型预训练模型 service_context = ServiceContext.from_defaults( chunk_size_limit=None, prompt_helper=PromptHelper(max_input_size=-1), llm_predictor=llm_predictor ) index = GPTSimpleVectorIndex(documents, service_context=service_context) query_engine = index.as_query_engine(similarity_top_k=2) response = query_engine.query("请描述一下图片里的人物表情特征") print(response) ``` 此段脚本展示了从加载本地目录下各类格式文件开始直到最终完成一次基于相似度排序后的top-k条目返回全过程。值得注意的是,“query”方法接收字符串参数代表使用者想要询问的内容,而在后台则会自动调用相应的解析模块并结合先前准备好的知识库来进行推理计算得出结论。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值